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盤啟動盤了怎麼辦怎麼恢複