java基礎之Servlet

定義

servlet是service applet的縮寫,即表示運行在服務端的應用。狹義的servlet是jdk中的一個接口,廣義的解釋實現了該接口的類。

繼承體系

在這裏插入圖片描述
顯而易見,這個體系比之前分析Spring的創建bean涉及的體系要簡單的多。
接下來我們單獨分析下這個體系涉及的接口

Servlet

package javax.servlet;

import java.io.IOException;

/**
 * Defines methods that all servlets must implement.
 *
 * <p>
 * A servlet is a small Java program that runs within a Web server. Servlets
 * receive and respond to requests from Web clients, usually across HTTP, the
 * HyperText Transfer Protocol.
 *
 * <p>
 * To implement this interface, you can write a generic servlet that extends
 * <code>javax.servlet.GenericServlet</code> or an HTTP servlet that extends
 * <code>javax.servlet.http.HttpServlet</code>.
 *
 * <p>
 * This interface defines methods to initialize a servlet, to service requests,
 * and to remove a servlet from the server. These are known as life-cycle
 * methods and are called in the following sequence:
 * <ol>
 * <li>The servlet is constructed, then initialized with the <code>init</code>
 * method.
 * <li>Any calls from clients to the <code>service</code> method are handled.
 * <li>The servlet is taken out of service, then destroyed with the
 * <code>destroy</code> method, then garbage collected and finalized.
 * </ol>
 *
 * <p>
 * In addition to the life-cycle methods, this interface provides the
 * <code>getServletConfig</code> method, which the servlet can use to get any
 * startup information, and the <code>getServletInfo</code> method, which allows
 * the servlet to return basic information about itself, such as author,
 * version, and copyright.
 *
 * @see GenericServlet
 * @see javax.servlet.http.HttpServlet
 */
public interface Servlet {

    public void init(ServletConfig config) throws ServletException;

    public ServletConfig getServletConfig();

    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException;

    public String getServletInfo();

    public void destroy();
}

其實jdk的介紹說的很清楚,我們現在對它總結下
1.所有的Servlets都必須實現這個接口,一個Servlets就是一個運行在web服務器中的小應用,這個應用大概幹了什麼勒?處理從web客戶端來的Request 然後返回一個Response。
2.這個接口定義了生命週期函數,如如何初始化這個Servlets,這個Servlets如何處理請求,這個Servlets如何從web服務器中被移除,此外,他還提供了獲取這個Servlets基本信息的方法,和獲取初始化信息的方法

ServletConfig

首先應該清楚,這個是對servlet級別的配置信息,這也是一種設計思路,許多框架都會對配置信息進行抽象。

package javax.servlet;

import java.util.Enumeration;

/**
 * A servlet configuration object used by a servlet container to pass
 * information to a servlet during initialization.
 */
public interface ServletConfig {
    
    //獲取名稱
    public String getServletName();
   
   //獲取容器	
    public ServletContext getServletContext();
    
    //根據初始化參數名稱獲取參數值
    public String getInitParameter(String name);

    //獲取初始化參數名稱
    public Enumeration<String> getInitParameterNames();
}

如我們在沒有SpringBoot前要寫Web.xml,假如用到了Spring Mvc 那麼我們要用如下的配置

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

那麼在在該Servlet在加載的時候就會把contextConfigLocation對應的值封裝到servlet config中去。

GenericServlet

這是一個與具體協議無關的抽象實現,它實現了ServletConfig接口中定義的方法,留下Service給具體的協議相關Servlet去實現

HttpServlet

HttpServlet是對GenericServlet的http協議的拓展
它核心幹了什麼事情勒?在運行器,它通過Service處理ServletRequest,它首先會把它轉換成HttpServletRequest,然後根據請求的方法類型,如post,get等來把請求路由到具體的doPost,doGet等方法中去。代碼如下

   protected void doPost(HttpServletRequest req, HttpServletResponse resp	throws ServletException, IOException{
	String protocol = req.getProtocol();
	String msg = lStrings.getString("http.method_post_not_supported");
	if (protocol.endsWith("1.1")) {
	    resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
	} else {
	    resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
	}
   }
    
  protected void service(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
    {
	String method = req.getMethod();

	if (method.equals(METHOD_GET)) {
	    long lastModified = getLastModified(req);
	    if (lastModified == -1) {
		// servlet doesn't support if-modified-since, no reason
		// to go through further expensive logic
		doGet(req, resp);
	    } else {
		long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
		if (ifModifiedSince < (lastModified / 1000 * 1000)) {
		    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
		    maybeSetLastModified(resp, lastModified);
		    doGet(req, resp);
		} else {
		    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
		}
	    }

	} else if (method.equals(METHOD_HEAD)) {
	    long lastModified = getLastModified(req);
	    maybeSetLastModified(resp, lastModified);
	    doHead(req, resp);

	} else if (method.equals(METHOD_POST)) {
	    doPost(req, resp);
	    
	} else if (method.equals(METHOD_PUT)) {
	    doPut(req, resp);	
	    
	} else if (method.equals(METHOD_DELETE)) {
	    doDelete(req, resp);
	    
	} else if (method.equals(METHOD_OPTIONS)) {
	    doOptions(req,resp);
	    
	} else if (method.equals(METHOD_TRACE)) {
	    doTrace(req,resp);
	    
	} else {
	    //
	    // Note that this means NO servlet supports whatever
	    // method was requested, anywhere on this server.
	    //

	    String errMsg = lStrings.getString("http.method_not_implemented");
	    Object[] errArgs = new Object[1];
	    errArgs[0] = method;
	    errMsg = MessageFormat.format(errMsg, errArgs);
	    
	    resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
	}
    }

我們在開發自己的Servlet時,我們直接繼承這個類就好了。
這個類也有意思,它是抽象的,但是卻沒有抽象方法,因爲doGet,doPost,doHead等等,我們通常不需要全都去實現,假如把它們都設置成抽象方法,那我們就不得不全都實現。現在我們只需要覆蓋我們自己像覆蓋的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章