一、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");
}


三、注意点

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

 

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