冰枫逸范
Victory won’t come to me unless I go to it. 胜利是不会向我走来的,我必须自己走向胜利。
在JSP中,跳转页面有两种方式:
1.forward跳转:
<jsp:forward page="跳转页面地址" />
2.response跳转:
response.sendRedirect("跳转页面地址");

两种跳转的区别如下:

1.forward跳转:
  a.服务器端跳转,地址栏不改变;
  b.执行到跳转语句后马上无条件跳转,之后的代码不再执行(跳转之前一定要释放全部资源);
  c.request设置的属性在跳转后的页面仍可以使用;
  d.使用<jsp:param name="参数名" value="参数值" />传递参数。

2.response跳转:
  a.客户端跳转,地址栏改变;
  b.所有代码执行完毕后跳转;
  c.跳转后的页面不能使用上一个页面的request属性;
  d.使用地址重写传递参数(response.sendRedirect("URL?参数名=参数值"))。

示例:   forward.jsp
 1 <%@ page contentType = "text/html; charset = gb2312"%>
 2 <%
 3     request.setCharacterEncoding("gb2312");
 4 %>
 5 
 6 <html>
 7     <head>
 8         <title> Forward.jsp </title>
 9     </head>
10 
11     <body>
12         <jsp:forward page = "jump.jsp" />
13     </body>
14 </html>
   
         response.jsp
 1 <%@ page contentType = "text/html; charset = gb2312"%>
 2 <%
 3     request.setCharacterEncoding("gb2312");
 4 %>
 5 
 6 <html>
 7     <head>
 8         <title> 跳转页面 </title>
 9     </head>
10 
11     <body>
12     <%
13         response.sendRedirect("jump.jsp");
14     %>
15     </body>
16 </html>

        jump.jsp
1 <html>
2     <head>
3         <title> 跳转页面 </title>
4     </head>
5 <body>
6     <h1> Hello Word!!! </h1>
7 </body>
8 </html>

可以发现使用forward跳转,地址栏的url没变;而使用response.sendRedirect()地址栏的url有变.
posted on 2008-04-06 13:18 冰枫逸范 阅读(967) 评论(0)  编辑  收藏 所属分类: JSP/Servlet

只有注册用户登录后才能发表评论。


网站导航: