阅读660 返回首页    go 阿里云 go 技术社区[云栖]


J2EE中RequestDispatcher.forward()和response.sendRedirect()的区别详谈

一、RequestDispatcher.forward()

在服务器端运行。采用采用请求转发,request对象始终存在,不会重新创建,前后页面共享同一个request。重定向后浏览器地址栏URL不变。

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
 response.setContentType("text/html; charset=gb2312");
 ServletContext sc = getServletContext();
 RequestDispatcher rd = null;
 rd = sc.getRequestDispatcher("/index.JSP");
 rd.forward(request, response);
}

 

二、response.sendRedirect()

在用户的浏览器端工作。重新定向,前后页面不共享一个request。重定向后在浏览器地址栏上会出现重定向页面的URL。

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
 response.setContentType("text/html; charset=gb2312");
 response.sendRedirect("/index.JSP");
}


三、注意点

其他一些注意点参看原帖地址的博文。


原帖地址:https://www.cnblogs.com/phpzxh/archive/2010/02/01/1661137.html


最后更新:2017-04-03 19:06:48

  上一篇:go DatagramSocket.receive()方法无响应的解决
  下一篇:go 雅安,挺住!