1.什麼是servlet轉發? servlet將用戶的請求通過一組servlet來處理,每個servlet都有自己特定的職責,當該servlet完成自己的工作後將請求轉發到下一個servlet來處理,這種通過不同servelet相互轉發請求來完成對某個請求的處理過程就叫做servlet轉發。 2.什 ...
1.什麼是servlet轉發?
servlet將用戶的請求通過一組servlet來處理,每個servlet都有自己特定的職責,當該servlet完成自己的工作後將請求轉發到下一個servlet來處理,這種通過不同servelet相互轉發請求來完成對某個請求的處理過程就叫做servlet轉發。
2.什麼是servlet包含?
servlet包含就是源servlet將其他web組件(servlet,html,jsp),生成的結果包含到自己的結果中。
3.為什麼需要servlet轉發和包含?
servlet對象是由Servlet容器創建,Servlet對象的Service()方法是由servlet容器調用,servlet之間是不允許相互調用(servlet API 2.1以前版本可以)。但是我們又需要通過多個servlet組件協調完成任務,同時方便代碼維護管理,提高開發效率。這樣就可以需要servlet轉發和包含的技術支持。
4、轉發和包含的相同點
(1)都是對同一個請求對象處理,源組件和目標組件共用同一個ServletRequest對象和ServletResponse對象。
(2)目標組件都可以是servlet,jsp,html。
(3)都依賴javax,servlet.RequestDispatcher 介面。
5.RequestDIspatcher 對象
該介面表示請求分發器
(1)獲取RequestDispatcher對象方法
調用ServletContext的getRequestDispather(String path)方法獲得。
調用ServletRequest的getRequestDispather(String path)方法獲得。
(2)兩種方法的區別
ServletContext調用方法的參數必須為絕對路徑,而ServletRequest調用方法參數可以是絕對路徑也可以是相對路徑。
6.請求轉發案例
CheckServlet 類用於處理用戶請求參數。
package com.learn; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/26. */ public class CheckServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userName = req.getParameter("name"); String message = ""; if(userName == null){ message = "please input your name"; } else { message = userName; } req.setAttribute("mess",message); RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/out"); PrintWriter out = resp.getWriter(); out.print("before forward "); System.out.println("before forward"); requestDispatcher.forward(req,resp); out.print("after forward"); System.out.println("after forward"); out.close(); } }
OutServlet 用於輸出處理結果
package com.learn; import org.json.HTTP; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/26. */ public class OutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String message = (String) req.getAttribute("mess"); PrintWriter printWriter = resp.getWriter(); printWriter.print(message); printWriter.close(); } }
註意: 1.在轉發之前不要提交響應結果。
2.轉發過程中,源組件的響應結果不會返回,只會返回目標組件的響應結果。
web.xml配置
<servlet> <servlet-name>check</servlet-name> <servlet-class>com.learn.CheckServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>check</servlet-name> <url-pattern>/check</url-pattern> </servlet-mapping> <servlet> <servlet-name>out</servlet-name> <servlet-class>com.learn.OutServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>out</servlet-name> <url-pattern>/out</url-pattern> </servlet-mapping>
7.包含(include)案例
MainServlet 用於包含其他組件,並返迴響應結果
package com.learn; import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/26. */ public class MainServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html><head><title>mainServlet</title></head>"); out.println("<body>"); ServletContext servletContext = getServletContext(); RequestDispatcher headDisPatcher = servletContext.getRequestDispatcher("/header.html"); RequestDispatcher greetDisPatcher = servletContext.getRequestDispatcher("/greet"); RequestDispatcher footeDisPatcher = servletContext.getRequestDispatcher("/footer.html"); headDisPatcher.include(req,resp); greetDisPatcher.include(req,resp); footeDisPatcher.include(req,resp); out.print("</body></html>"); out.close(); } }
header.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>header</title> </head> <body> <font size="6">Welcome to ab</font> </body> </html>
GreetServlet
package com.learn; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/26. */ public class GreetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out = resp.getWriter(); out.print("hi! "+req.getParameter("name")+", <p>"); out.close(); } }
footer.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>footer</title> </head> <body> <span>Thanks for stopping</span> </body> </html>
web.xm配置
<servlet> <servlet-name>main</servlet-name> <servlet-class>com.learn.MainServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>main</servlet-name> <url-pattern>/main</url-pattern> </servlet-mapping> <servlet> <servlet-name>greet</servlet-name> <servlet-class>com.learn.GreetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>greet</servlet-name> <url-pattern>/greet</url-pattern> </servlet-mapping>