您的位置:首页 > 产品设计 > UI/UE

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

2013-04-24 12:42 429 查看
一、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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐