Servlet-继承结构

Servlet-继承结构

  • Servlet接口
  • GenericServlet抽象类:实现了Servlet接口
  • HttpServlet抽象类:继承了GenericServlet抽象类

Servlet接口

package jakarta.servlet;

import java.io.IOException;

public interface Servlet {
    //实例化对象后对对象进行初始化
    void init(ServletConfig var1) throws ServletException;
	//获取Servlet配置文件,返回一个ServletConfig对象
    ServletConfig getServletConfig();
	//处理用户请求的方法
    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

    String getServletInfo();

    void destroy();
}


GenericServlet抽象类

在GenericServlet抽象类中,着重实现的是除service方法外的其他方法的基础配置

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {	
   
    private transient ServletConfig config;
    
    //初始化方法
	public void init(ServletConfig config) throws ServletException {
   		this.config = config;
    	this.init();
	}
    
    //初始化方法的重载
	public void init() throws ServletException {
	}
    
    //这里的service仍为抽象方法
	public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
    
    //返回ServletConfig配置文件对象
    public ServletConfig getServletConfig() {
        return this.config;
    }
    
    public void destroy() {
    }

}

HttpServlet抽象类

在GenericServlet抽象类中,着重实现的是service方法

在这里,一部分程序员喜欢重写service方法,另一部分习惯于重写do…方法

但这并不重要,因为在springMVC中,无需自己配置业务方法😓

public abstract class HttpServlet extends GenericServlet {
    //这个service方法将ServletRequest、ServletResponse两个对象转为其子类对象HttpServletRequest、HttpServletResponse(父转子)
    //并调用其重载的service方法
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        HttpServletRequest request;
        HttpServletResponse response;
        try {
            request = (HttpServletRequest)req;
            response = (HttpServletResponse)res;
        } catch (ClassCastException var6) {
            throw new ServletException(lStrings.getString("http.non_http"));
        }

        this.service(request, response);
    }
   	//重载的service方法,对请求的方式进行区分,在分别调用其处理业务的方法
     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        if (method.equals("GET")) {
        	this.doGet(req, resp);
        } else if (method.equals("HEAD")) {
            this.doHead(req, resp);
        } else if (method.equals("POST")) {
            this.doPost(req, resp);
        } else if (method.equals("PUT")) {
            this.doPut(req, resp);
        } else if (method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if (method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if (method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            resp.sendError(501, errMsg);
        }
    }
    //默认情况下,所有service调用的do…方法都是返回“此方法不被支持”
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String msg = lStrings.getString("http.method_get_not_supported");
        this.sendMethodNotAllowed(req, resp, msg);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String msg = lStrings.getString("http.method_post_not_supported");
        this.sendMethodNotAllowed(req, resp, msg);
    }

}