| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > WEB开发

JSP三种页面跳转方式的比较

时间:2006-06-09 11:47:23  来源:Linux联盟收集  作者:Linux联盟收集

使用JSP大约有下列三种跳转方式:XjgLinux联盟
1. response.sendRedirect();XjgLinux联盟
2. response.setHeader("Location","");XjgLinux联盟
3. <jsp:forward page="" /> XjgLinux联盟

经过试验得到下面的一些规则:XjgLinux联盟

一. response.sendRedirect()XjgLinux联盟

此语句前不允许有out.flush(),如果有,会有异常:XjgLinux联盟
java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client.XjgLinux联盟
 at com.caucho.server.connection.AbstractHttpResponse.sendRedirect(AbstractHttpResponse.java:558)XjgLinux联盟
...XjgLinux联盟
跳转后浏览器地址栏变化XjgLinux联盟
如果要跳到不同主机下,跳转后,此语句后面的语句会继续执行,如同新开了线程,但是对response的操作已经无意义了;XjgLinux联盟
如果要跳到相同主机下,此语句后面的语句执行完成后才会跳转;XjgLinux联盟
二. response.setHeader("Location","")XjgLinux联盟

此语句前不允许有out.flush(),如果有,页面不会跳转。XjgLinux联盟
跳转后浏览器地址栏变化XjgLinux联盟
此语句后面的语句执行完成后才会跳转XjgLinux联盟
三. <jsp:forward page="" /> XjgLinux联盟

此语句前不允许有out.flush(),如果有,会有异常:XjgLinux联盟
java.lang.IllegalStateException: forward() not allowed after buffer has committed.XjgLinux联盟
 at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:134)XjgLinux联盟
 at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:101)XjgLinux联盟
 at com.caucho.jsp.PageContextImpl.forward(PageContextImpl.java:836)XjgLinux联盟
 ...XjgLinux联盟
跳转后浏览器地址栏不变,但是只能跳到当前主机下XjgLinux联盟
此语句后面的语句执行完成后才会跳转XjgLinux联盟
XjgLinux联盟

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
    无相关信息
栏目更新
栏目热门