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
爲了得到更好的閱讀體驗,請訪問原博客地址。
限於本人水平,若是文章和代碼有表述不當之處,還請不吝賜教。
代碼鋼琴家