RequestDispatcher简介
public interface RequestDispatcher
{
public void forward(ServletRequest request, ServletResponse response)
throws ServletException, IOException; public void include(ServletRequest request, ServletResponse response)
throws ServletException, IOException;
}
RequestDispatcher.forward(request, response)
public class User{
private String name;
private int age;
public String getName(){
return name ;
}
public void setName( String name ){
this .name = name ;
}
public int getAge() {
return age ;
}
public void setAge( int age ){
this .age = age ;
}
}
UsersServlet.java
public class UsersServlet extends HttpServlet {
private static final long serialVersionUID = 1L ; protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
/*****************一般实际开发这些用户数据都是从数据库查出来的*********/
List <User > users = new ArrayList <> ();
User u1 = new User () ;
u1 .setAge ( 20) ;
u1 .setName ( "Bob") ;
User u2 = new User () ;
u2 .setAge ( 21) ;
u2 .setName ( "Tony") ;
users .add ( u1) ;
users .add ( u2) ;
/*********************************************/
request .setAttribute ( "users", users) ; //对request 进制预处理准备工作
request .getRequestDispatcher ( "users.jsp").forward( request , response );//转发到users.jsp,让他去具体响应
}
}
<%@ page contentType= "text/html; charset=UTF-8" pageEncoding ="UTF-8" trimDirectiveWhitespaces= "true"
session ="true" %>
<%@ taglib prefix= "c" uri = "http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html>
< html>
<head>
<meta http-equiv = "Content-Type" content ="text/html; charset=UTF-8">
<title> 用户列表</title>
</head>
<body> <p> -----------------转发到的资源users.jsp产生的响应数据------------------ </p> < c:forEach var ="user" items= " ${users}" >
用户姓名:${user.name} 用户年龄:${user.age} <br />
</ c:forEach>
</body>
</html>
例子2:不使用Attribute,使用Paramter向转发的资源传递参数。
虽然request对象没有setParameter方法来设置参数,但是我们可以在转发的URL后通过QueryString 的方式添加。JSP中的<jsp:foward>标签下的<jsp:param>标签就是使用的这个原理。
AimServlet.java
public class AimServlet extends HttpServlet {
private static final long serialVersionUID = 1L ; protected void doGet( HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException { request .getRequestDispatcher ( "foo.jsp?num=1") . forward( request , response );
}
}
foo.jsp
<%@ page contentType= "text/html; charset=UTF-8" pageEncoding ="UTF-8" trimDirectiveWhitespaces= "true"
session ="true" %>
<%@ taglib prefix= "c" uri = "http://java.sun.com/jsp/jstl/core" %> <! DOCTYPE html>
<html>
<head>
<meta http-equiv = "Content-Type" content ="text/html; charset=UTF-8">
<title> 标题</title>
</head>
<body> 通过forward传递过来的参num=${param.num} </body>
</html>
RequestDispatcher.include(request, response)
注意事项:
1、被包含者不能设置ServletResponse的响应状态和响应头(否则并不会产生效果),因为这些都是包含者做的事,被包含者只需要产生响应数据解可以了。
2、不同于 forward中的request的传递特性:在被包含的资源中从request中获取请求路径相关的信息,发现依然是原始请求的路径,也就是浏览器地址栏相关的路径,也就是说被包含的资源获得的request对象的路径属性和原始请求资源的路径一样(见下面的例子1)。其它的API调用也是一样的(Attribute 和Parameter)。
例子1
TargetServlet.java
public class TargetServlet extends HttpServlet {
private static final long serialVersionUID = 1L ;
protected void doGet( HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException { response .setContentType ( "text/html;charset=utf-8" );
PrintWriter out = response .getWriter () ; out .println ( "----------来自TargetServlet的告白----------------<br />" ) ;
out .print ( "我偷懒了,下面的响应数据并不是我自己产生的,而是包含的其它资源产生的<br/>" ) ;
request .getRequestDispatcher ( "test.jsp") . include( request , response ); out .flush () ;
out .close () ;
}
}
test.jsp
<%@ page contentType= "text/html; charset=UTF-8" pageEncoding = "UTF-8" trimDirectiveWhitespaces = "true"
session = "false"
%> <p> ------------------------来自test.jsp的告白-------------------------- </p>
<p> 我输出的响应数据将被其它的资源包含 </p>
请的URL是 <%= request.getRequestURL().toString() %> ,可以看出客户端真正请求的不是我,我只是幕后工作者。
<p> 但我很开心,因为响应给客户端的数据一部分来自于我 </p>
例子2:通过包含路径后追加QueryString来向被包含资源传递参数,以及通过request.setAttribute传递属性。
同样, JSP中的<jsp:include>标签下的<jsp:param>标签就是通过在含路径后追加QueryString达到的传递参数的效果。
public class TargetServlet extends HttpServlet {
private static final long serialVersionUID = 1L ;
protected void doGet( HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException { response .setContentType ( "text/html;charset=utf-8" );
PrintWriter out = response .getWriter () ; out .println ( "----------来自TargetServlet的告白----------------<br />" ) ;
out .print ( "我偷懒了,下面的响应数据并不是我自己产生的,而是包含的其它资源产生的<br/>" ) ; request .setAttribute ( "sharedatt", "I`m shared attribute") ; request .getRequestDispatcher ( "test.jsp?sharedparam=Im-shared-parameter" ). include (request , response ) ; out .flush () ;
out .close () ;
}
}
<%@ page contentType= "text/html; charset=UTF-8" pageEncoding = "UTF-8" trimDirectiveWhitespaces = "true"
session = "false"
%> <p> ------------------------来自test.jsp的告白-------------------------- </p>
<p> 我输出的响应数据将被其它的资源包含 </p>
<p> 从request中提取共享的属性Attribute : <%= request.getAttribute("s haredatt") %>
<p> 从request中提取共享的参数Parameter : <%= request.getParameter("sharedparam" ) %>
欢迎转载,请注明出处:www.cnblogs.com/lulipro
为了获得更好的阅读体验,请访问原博客地址。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
代码钢琴家