閱讀703 返回首頁    go 小米 go 小米5


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:13:18

  上一篇:go C語言中如何在main函數開始前執行函數
  下一篇:go MaxDot照片秀(on github page)