本文源碼: GitHub·點這裏 || GitEE·點這裏
Servlet是JavaWeb的三大組件之一(Servlet、Filter、Listener),它屬於動態資源。Servlet的做用是處理請求,服務器會把接收到的請求交給Servlet來處理,在Servlet中一般須要:接收請求數據;處理請求;完成響應。html
API | 做用描述 |
---|---|
ServletConfig | 獲取servlet初始化參數和servletContext對象。 |
ServletContext | 在整個Web應用的動態資源之間共享數據。 |
ServletRequest | 封裝Http請求信息,在請求時建立。 |
ServletResponse | 封裝Http響應信息,在請求時建立。 |
容器在初始化servlet時,爲該servlet建立一個servletConfig對象,並將這個對象經過init()方法來傳遞並保存在此Servlet對象中。核心做用:1.獲取初始化信息;2.獲取ServletContext對象。java
<servlet> <init-param> <param-name>my-name</param-name> <param-value>cicada</param-value> </init-param> <servlet-name>servletOneImpl</servlet-name> <servlet-class>com.node02.servlet.impl.ServletOneImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletOneImpl</servlet-name> <url-pattern>/servletOneImpl</url-pattern> </servlet-mapping>
public class ServletOneImpl implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { String servletName = servletConfig.getServletName() ; System.out.println("servletName="+servletName); String myName = servletConfig.getInitParameter("my-name") ; System.out.println("myName="+myName); Enumeration paramNames = servletConfig.getInitParameterNames() ; while (paramNames.hasMoreElements()){ String paramKey = String.valueOf(paramNames.nextElement()) ; String paramValue = servletConfig.getInitParameter(paramKey) ; System.out.println("paramKey="+paramKey+";paramValue="+paramValue); } ServletContext servletContext = servletConfig.getServletContext() ; servletContext.setAttribute("cicada","smile"); } }
一個項目只有一個ServletContext對象,能夠在多個Servlet中來獲取這個對象,使用它能夠給多個Servlet傳遞數據,該對象在Tomcat啓動時就建立,在Tomcat關閉時纔會銷燬!做用是在整個Web應用的動態資源之間共享數據。node
一、ServletConfig#getServletContext(); 二、GenericServlet#getServletContext(); 三、HttpSession#getServletContext() 四、ServletContextEvent#getServletContext()
ServletContext是JavaWeb四大域對象之一:git
一、PageContext; 二、ServletRequest; 三、HttpSession; 四、ServletContext;
全部域對象都有存取數據的功能,由於域對象內部有一個Map,用來存儲數據。github
<context-param> <param-name>my-blog</param-name> <param-value>2019-11-19</param-value> </context-param> <servlet> <servlet-name>servletTwoImpl</servlet-name> <servlet-class>com.node02.servlet.impl.ServletTwoImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletTwoImpl</servlet-name> <url-pattern>/servletTwoImpl</url-pattern> </servlet-mapping>
public class ServletTwoImpl extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); // 一、參數傳遞 ServletContext servletContext = this.getServletContext() ; String value = String.valueOf(servletContext.getAttribute("cicada")) ; System.out.println("value="+value); // 二、獲取初始化參數 String myBlog = servletContext.getInitParameter("my-blog") ; System.out.println("myBlog="+myBlog); // 三、獲取應用信息 String servletContextName = servletContext.getServletContextName() ; System.out.println("servletContextName="+servletContextName); // 四、獲取路徑 String pathOne = servletContext.getRealPath("/") ; String pathTwo = servletContext.getRealPath("/WEB-INF/") ; System.out.println("pathOne="+pathOne+";pathTwo="+pathTwo); response.getWriter().print("執行:doGet; value:"+value); } }
HttpServletRequest接口繼承ServletRequest接口,用於封裝請求信息,該對象在用戶每次請求servlet時建立並傳入servlet的service()方法,在該方法中,傳入的servletRequest將會被強制轉化爲HttpservletRequest對象來進行HTTP請求信息的處理。核心做用:1.獲取請求報文信息;2.獲取網絡鏈接信息;3.獲取請求域屬性信息。segmentfault
<servlet> <servlet-name>servletThreeImpl</servlet-name> <servlet-class>com.node02.servlet.impl.ServletThreeImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletThreeImpl</servlet-name> <url-pattern>/servletThreeImpl</url-pattern> </servlet-mapping>
public class ServletThreeImpl extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // http://localhost:6003/servletThreeImpl?myName=cicada String method = request.getMethod(); System.out.println("method="+method); // GET String requestURL = request.getRequestURL().toString(); // http://localhost:6003/servletThreeImpl System.out.println("requestURL="+requestURL); String requestURI = request.getRequestURI(); System.out.println("requestURI="+requestURI); // /servletThreeImpl String queryString = request.getQueryString() ; System.out.println("queryString="+queryString); // myName=cicada String myName = request.getParameter("myName"); System.out.println("myName="+myName); // cicada } }
HttpServletResponse繼承自ServletResponse,封裝了Http響應信息。客戶端每一個請求,服務器都會建立一個response對象,並傳入給Servlet.service()方法。核心做用:1.設置響應頭信息;2.發送狀態碼;3.設置響應正文;4.重定向;瀏覽器
<servlet> <servlet-name>servletFourImpl</servlet-name> <servlet-class>com.node02.servlet.impl.ServletFourImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletFourImpl</servlet-name> <url-pattern>/servletFourImpl</url-pattern> </servlet-mapping>
public class ServletFourImpl extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8") ; response.setCharacterEncoding("UTF-8"); response.setStatus(200) ; response.getWriter().print("Hello,知了"); } }
服務器端進行的頁面跳轉的控制 ;服務器
request.getRequestDispatcher("/轉發地址").forward(request, response);
服務端響應跳轉信息,瀏覽器端進行的頁面跳轉 ;網絡
response.sendRedirect("重定向地址");
區別 | 轉發 | 重定向 |
---|---|---|
地址欄 | 不變 | 變化 |
跳轉 | 服務端跳轉 | 瀏覽器端跳轉 |
請求次數 | 一次 | 兩次 |
域中數據 | 不會丟失 | 丟失 |
GitHub·地址 https://github.com/cicadasmile/java-base-parent GitEE·地址 https://gitee.com/cicadasmile/java-base-parent