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等等,我们通常不需要全都去实现,假如把它们都设置成抽象方法,那我们就不得不全都实现。现在我们只需要覆盖我们自己像覆盖的。

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