servlet 页面跳转

Servlet
当然,在servlet中,一般跳生在doGet, doPost等方法里面。

1)  redirect
方式

response.sendRedirect("/a.jsp");
面的路径是相路径。sendRedirect可以将面跳到任何面,不一定局限于本web
用中,如:
response.sendRedirect("http://www.ycul.com");

浏览器地址栏变化。
这种方式要传值出去的,只能在urlparameter或者放在session中,无法使用request.setAttribute传递

2) forward
方式

RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");
dispatcher .forward(request, response);
面的路径是相路径。forward方式只能跳到本web用中的
面上。

浏览器地址不会化。
使用这种方式跳传值可以使用三方法:urlparametersessionrequest.setAttribute

 

3)插入<a href=url></a>,页面跳转。?!

JSP

1)  response.sendRedirect();
servletresponse.sendRedirect()方式一


句前不允out.flush(),如果有,会有异常:

java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client.
at com.caucho.server.connection.AbstractHttpResponse.sendRedirect(AbstractHttpResponse.java:558)
...
浏览器地址栏变

如果要跳到不同主机下,跳后,此句后面的句会继续执行,如同新线程,但是response的操作已无意了;

如果要跳到相同主机下,此句后面的行完成后才会跳



2)  response.setHeader("Location","");
句前不允out.flush(),如果有,面不会跳

浏览器地址栏变

句后面的行完成后才会跳


3)  <jsp:forward page="" />
句前不允out.flush(),如果有,会有异常:

java.lang.IllegalStateException: forward() not allowed after buffer has committed.
at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:134)
at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:101)
at com.caucho.jsp.PageContextImpl.forward(PageContextImpl.java:836)
...
浏览器地址
,但是只能跳到当前主机下
句后面的行完成后才会跳



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值