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
上一篇:
DatagramSocket.receive()方法无响应的解决
下一篇:
雅安,挺住!
【Xenserver】(一)新建CIFS iso库
想找回丢在出租车的手机?你需要融合异构数据的城市级查询和推理
Android模块化编译
LLVM在静态分析上的增强 @ WWDC 2013
<font color="red">[置顶]</font>
Free Apache Tomcat Hosting in the Cloud for Java Applications? It's Called OpenShift!
java与c/c++之间的数据交互-----jni点滴【转】
Mysql日期和时间函数总结
《iPhone与iPad开发实战—iOS经典应用剖析》连载四
电脑不识别U盘启动盘了怎么办怎么恢复