Servlet接口

  • Servlet接口中有5个方法,其中三个生命周期方法、两个普通方法
  1. 初始化:执行init()方法,Servlet生命周期中,仅执行一次init()方法,它是在服务器装入Servlet时执行的,即第一次访问这个Servlet才执行
  2. 服务:执行service()方法,service()方法是Servlet的核心。每当一个客户请求HttpServlet对象,该对象的service()方法就要被调用
  3. 销毁:执行destroy()方法,destroy()方法仅执行一次,即在服务器停止且卸载Servlet时执行该方法
public interface Servlet {
    void init(ServletConfig var1) throws ServletException;

    ServletConfig getServletConfig();

    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

    String getServletInfo();

    void destroy();
}

示?

  • 继承关系:UserServlet->HttpServlet->GenericServlet->implements Servlet
  • HttpServlet里面实现了service()方法,里面会根据不同的http method调用不同的方法,所以我们自定义servlet只需要重写对应的doXXX方法即可
  • 以下示例:第一次访问userServletdoGet()方法时,init()方法被调用;接下来再次调用userServlet中的方法时不会再调用init()方法;将服务停止会发现destroy()方法被调用
    package net.fl.web;
    
    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;
    
    /**
     * @author fl
     * @description: TODO
     * @date 2022/5/13 2:36 下午
     */
    public class UserServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("doGet");
            //设置响应内容
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter printWriter = resp.getWriter();
            printWriter.write("<div>doGet可以了</div>");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("doPost");
            //设置响应内容
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter printWriter = resp.getWriter();
            printWriter.write("<div>doPost可以了</div>");
        }
    
        @Override
        public void init() throws ServletException {
            System.out.println("servlet init");
        }
    
        @Override
        public void destroy() {
            System.out.println("servlet destroy");
        }
    }
    
    
    Servlet
  • HttpServlet 源码中service()方法
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String method = req.getMethod();
            long lastModified;
            if (method.equals("GET")) {
                lastModified = this.getLastModified(req);
                if (lastModified == -1L) {
                    this.doGet(req, resp);
                } else {
                    long ifModifiedSince = req.getDateHeader("If-Modified-Since");
                    if (ifModifiedSince < lastModified / 1000L * 1000L) {
                        this.maybeSetLastModified(resp, lastModified);
                        this.doGet(req, resp);
                    } else {
                        resp.setStatus(304);
                    }
                }
            } else if (method.equals("HEAD")) {
                lastModified = this.getLastModified(req);
                this.maybeSetLastModified(resp, lastModified);
                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 {
                String errMsg = lStrings.getString("http.method_not_implemented");
                Object[] errArgs = new Object[]{method};
                errMsg = MessageFormat.format(errMsg, errArgs);
                resp.sendError(501, errMsg);
            }
    
        }
    
    
        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("non-HTTP request or response");
            }
    
            this.service(request, response);
        }