public interface RequestDispatcher { public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException; public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException; }
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.javahtml
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向转发的资源传递参数。java
虽然request对象没有setParameter方法来设置参数,可是咱们能够在转发的URL后经过QueryString 的方式添加。JSP中的<jsp:foward>标签下的<jsp:param>标签就是使用的这个原理。web
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>
注意事项:服务器
一、被包含者不能设置ServletResponse的响应状态和响应头(不然并不会产生效果),由于这些都是包含者作的事,被包含者只须要产生响应数据解能够了。session
二、不一样于 forward中的request的传递特性:在被包含的资源中从request中获取请求路径相关的信息,发现依然是原始请求的路径,也就是浏览器地址栏相关的路径,也就是说被包含的资源得到的request对象的路径属性和原始请求资源的路径同样(见下面的例子1)。其它的API调用也是同样的(Attribute 和Parameter)。jsp
例子1ui
TargetServlet.javathis
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
为了得到更好的阅读体验,请访问原博客地址。
限于本人水平,若是文章和代码有表述不当之处,还请不吝赐教。
代码钢琴家