免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 658 | 回复: 0
打印 上一主题 下一主题

jsp中forward和sendRedirect的区别 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-20 15:49 |只看该作者 |倒序浏览
1.RequestDispatcher.forward()
是在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个Servlet,JSP 或普通HTML文件,也即你的form提交至a.jsp,在a.jsp用到了forward()重定向至b.jsp,此时form提交的所有信息在 b.jsp都可以获得,参数自动传递.
但forward ()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,同时forward()无法在后面带参数传递,比如 servlet?name=frank,这样不行,可以程序内通过response.setAttribute("name",name)来传至下一个页面.
重定向后浏览器地址栏URL不变.
例:servlet文件中重定向
CODE
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);
}
2.response.sendRedirect()
是在用户的浏览器端工作,sendRedirect()可以带参数传递,比如servlet?name=frank传至下个页面,同时它可以重定向至不同的主机上,且在浏览器地址栏上会出现重定向页面的URL.
sendRedirect()可以重定向有frame的jsp文件.
例:servlet文件中重定向
CODE
public void doPost(HttpServletRequest request,HttpServletResponse response)
       throws ServletException,IOException
{
       response.setContentType("text/html; charset=gb2312");
       response.sendRedirect("/index.jsp");
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/28292/showart_218662.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP