posts - 37, comments - 8, trackbacks - 0, articles - 0

request之请求转发

Posted on 2008-10-19 14:36 梦与桥 阅读(7152) 评论(2)  编辑  收藏 所属分类: jsp程序设计
属于同一个Web应用程序的JSP或Servlet可以使用javax.servlet.RequestDispatcher接口的forward方法和include方法来共享数据。
    §RequestDispatcher rd=request.getRequestDispatcher(String path):取得一个相对于当前路径的请求转发器,以便于请求转发。
     §void forward(ServletRequest request, ServletResponse response):用于将一个请求从一个JSP或servlet转发到同一服务器上的另一个JSP或servlet。
    §void include ():用于包括另一个Servlet的内容。

1、利用forward()方法作转发控制
(包括请求页面a0.jsp、接受请求的页面a1.jsp和转发到页面a2.sjp)
a0.jsp的code如下:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
请求参数输入页面
</title>
</head>
<body bgcolor="#ffffff">
<h1>
请求参数输入页面
</h1>
<form action="a0.jsp" method="POST">
  
<table border="1">
    
<tr>
      
<td>用户名:</td>
      
<td><input  type="text" name="name"/></td>
    
</tr>
    
<tr>
      
<td>性别:</td>
      
<td>
        
<input  type="radio" name="sex" value="男"/>
        
<input  type="radio" name="sex" value="女"/>
      
</td>
    
</tr>
    
<tr>
      
<td>兴趣爱好:</td>
      
<td>
        
<input  type="checkbox" name="interest" value="上网"/>上网
        
<input  type="checkbox" name="interest" value="旅游"/>旅游
        
<input  type="checkbox" name="interest" value="阅读"/>阅读
      
</td>
    
</tr>
  
</table>
  
<input  type="submit" name="submit" value="提交"/>
</form>
</body>
</html>
a1.jsp的code如下:
<%@ page contentType="text/html; charset=GBK"%>
<html>
<head>
<title>接收请求参数</title>
</head>
<body bgcolor="#ffffff">
<h1>接收客户端请求数据</h1>
<%
    request.setCharacterEncoding(
"GBK");
    
String name = request.getParameter("name");
    
String sex = request.getParameter("sex");
    
String[] interest = request.getParameterValues("interest");
%>
<table border="1">
    
<tr>
        
<td>用户名:</td>
        
<td><%=name%></td>
    
</tr>
    
<tr>
        
<td>性别:</td>
        
<td><%=sex%></td>
    
</tr>
    
<tr>
        
<td>兴趣爱好:</td>
        
<td>
        
<%
                
for (int i = 0; i < interest.length; i++) {
                out.print(interest[i]);
                out.print(
",");
            }
        
%>
        
</td>
    
</tr>
</table>
<%
    RequestDispatcher rd
=request.getRequestDispatcher("ra3.jsp");
    rd.forward(request,response);
%>
</body>
</html>
a2.jsp的code如下:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
dispatcher转发结果显示页面
</title>
</head>
<body bgcolor="#ffffff">
<h1>
dispatcher转发结果显示页面
</h1>
name参数的值是:
<%=request.getParameter("name")%>
</body>
</html>

2、利用include()方法实现转发控制 (包括请求页面a0.jsp、接受请求的页面a1.jsp和转发到页面a2.sjp)
a0.jsp与a2.jsp同forward()方法转发,a1.jsp的code如下:
<%@ page contentType="text/html; charset=GBK"%>
<html>
<head>
<title>接收请求参数</title>
</head>
<body bgcolor="#ffffff">
<h1>接收客户端请求数据</h1>
<%
    request.setCharacterEncoding(
"GBK");
    
String name = request.getParameter("name");
    
String sex = request.getParameter("sex");
    
String[] interest = request.getParameterValues("interest");
%>
<table border="1">
    
<tr>
        
<td>用户名:</td>
        
<td><%=name%></td>
    
</tr>
    
<tr>
        
<td>性别:</td>
        
<td><%=sex%></td>
    
</tr>
    
<tr>
        
<td>兴趣爱好:</td>
        
<td>
        
<%
                
for (int i = 0; i < interest.length; i++) {
                out.print(interest[i]);
                out.print(
",");
            }
        
%>
        
</td>
    
</tr>
</table>
<%
    out.flush();
//不影响
    RequestDispatcher rd 
= request.getRequestDispatcher("ra3.jsp");
    rd.include(request, response);
       out.println("我又回来了~~");
%>
</body>
</html>
注意:调用forward()方法后,原先存放在HttpResponse对象中的内容将会被自动清除,再回应被发送到客户端之前才能调用forward()方法。如在out.flush()与out.close()之后使用,当无效。forward()方法与include方法非常类似,前者只显示被转发到的显面,后者将两个页面的内容合并在一起显示,并回到转发前的页面。
 

3、requestDispatcher.forward()和response.sendRedirect()两者的区别是什么,平时如何在程序开发中如

何使用?
答:requestDispatcher.forward()是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后

的地址;而response.sendRedirect()则是完全的跳转,浏览器会得到跳转的地址,并重新发送请求链接

。这样,从浏览器的地址栏中可以看到跳转后的链接地址。前者更加高效,在前者可以满足需要时,昼使

用它。

Feedback

# re: request之请求转发  回复  更多评论   

2013-07-08 12:59 by 46456
456465

# re: request之请求转发  回复  更多评论   

2013-07-08 13:00 by 46456
…………
我以为这是代码测试的

2了

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


网站导航: