转发、重定向
目录
- 1、转发
- 2、重定向
1、转发
只有一个request请求
package com.easy.web.servlet;import com.easy.web.pojo.Student;
import com.easy.web.util.JDBCUtil;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;//取名字用于访问
@WebServlet("/student")
//继承什么就是什么
public class StudentServlet extends HttpServlet {//访问servlet的时候默认访问service方法//需要重写父类中的service方法@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("StudentServlet.service");Connection connection = null;PreparedStatement preparedStatement = null;ResultSet resultSet = null;//扩大作用域,下面要将list数据放到req中List<Student> list = new ArrayList<>();try {//利用工具类方法获取连接connection = JDBCUtil.getConnection();String sql = "SELECT id,name,age,gender FROM student";preparedStatement = connection.prepareStatement(sql);resultSet = preparedStatement.executeQuery();//因为在这里声明的list只能在try的大括号里面使用,下面拿不到,所以需要在try上面声明list,扩大作用域//List<Student> list = new ArrayList<>();while (resultSet.next()) {int id = resultSet.getInt("id");String name = resultSet.getString("name");int age = resultSet.getInt("age");String gender = resultSet.getString("gender");Student student = new Student(id, name, age, gender);list.add(student);}for (Student student : list) {System.out.println(student);}}//ClassNotFoundException在JDBCUtil静态代码块中已被捕获,所以不需要在这里再捕获了catch (SQLException e) {throw new RuntimeException(e);} finally {JDBCUtil.close(connection, preparedStatement, resultSet);}//将list数据放入req//(设置属性)req.setAttribute("list",list);//转发到student_list.jsp页面进行展示//(把请求分发)req.getRequestDispatcher("student_list.jsp").forward(req,resp);}
}
2、重定向
存在两个request请求
package com.easy.web.servlet;import com.easy.web.util.JDBCUtil;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;@WebServlet("/deleteStudent")
public class DeleteStudentServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//打印方法名,即使结果没有达到预期效果,你也可以知道是否进行了正常的访问System.out.println("DeleteStudentServlet.service");//访问delteStudent之后,我们需要拿到id值才能去删除//拿任何参数都是到req中通过getParameter拿,返回值都是String类型String id = req.getParameter("id");Connection connection = null;PreparedStatement preparedStatement = null;//不是查询类,没有结果集try {connection = JDBCUtil.getConnection();String sql = "DELETE FROM student WHERE id=?";//一个一个填补括号中的“?”preparedStatement = connection.prepareStatement(sql);//用Integer.parseInt(id)将id转为int类型preparedStatement.setInt(1,Integer.parseInt(id));//打印出来,显示在执行什么语句(非必须)System.out.println(preparedStatement);//返回影响行数int count = preparedStatement.executeUpdate();System.out.println("count: " + count);} catch (SQLException e) {throw new RuntimeException(e);} finally {JDBCUtil.close(connection,preparedStatement,null);}//重定向 302请求//通过resp将重定向的地址告诉浏览器,浏览器自动发送请求//修改(添加、删除)完数据后重定向到“student”页面,将修改后的数据展示出来//即修改数据后,又会到StudentServlet,自动查找所有数据,转发到页面进行展示resp.sendRedirect("/student");}
}