RequestDispatcher.forward() 方法和HttpServletResponse.sendRedirect()方法的区别

时间:2021-11-13 20:12:49

RequestDispatcher.forward() 方法和HttpServletResponse.sendRedirect()方法的区别

先贴一段代码

    public void logon(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        RequestDispatcher rd=null;

        LoginJdbc login=new LoginJdbc();
Student stu = new Student();
stu.setNum(request.getParameter("userNum"));
stu.setPassword(request.getParameter("userPass")); boolean mark = login.login(stu); //获取登入标记 if(!mark){
response.sendRedirect("2/error.jsp");
}
else{
rd=request.getRequestDispatcher("2/Registration.jsp");
rd.forward(request,response);
} }

代码中用到了RequestDispatcher.forward() 方法和HttpServletResponse.sendRedirect()方法,所以我就在思考着俩个方法有什么区别,然后通过运用我的代码,总结了一下表格:

RequestDispatcher.forward() 方法 HttpServletResponse.sendRedirect()方法
运行在服务端 运行在客户端
浏览器url地址不变化

浏览器url地址变化                 

转载于:https://www.cnblogs.com/qisong178878915/p/4152094.html