java - servlet life cycle

Posted by burnside on Wed, 05 Jan 2022 23:36:06 +0100

1.Servlet life cycle

Servlet has no main() method and cannot run independently. Its operation is completely controlled and scheduled by servlet engine. The so-called life cycle refers to the whole process of when the servlet container creates a servlet instance, when it calls its methods to process requests, and when it and destroys its instances.

-Instance and initialization timing

When the request reaches the container, the container looks for the existence of the servlet object. If it does not exist, an instance will be created and initialized

-Ready / invoke / service phase

When a request arrives at the container, the container calls the service() method of the servlet object, and the method for processing the request can be called many times in the whole life cycle; The service() method of HttpServlet will call the doGet() or doPost() method according to the request method. However, by default, these two do methods throw exceptions and need subclasses to override.

-Destruction timing

When the container closes (when the application stops), the Servlet instance in the application is destroyed.
The above life cycle can be observed through the life cycle method in Servlet. There are three life cycle methods in the Servlet, which are not called manually by the user, but automatically called by the container at a specific time. Observe these three life cycle methods to observe the life cycle of the Servlet.

2. Three life cycles and three methods

init method, which is executed after the Servlet instance is created (proving that an instance of the Servlet has been created)

public void init(servletconfig config) throws serv1etException {
system.out.println("Instance created...");

The service method is executed every time a request reaches a Servlet method to process the request (proving that the Servlet has served)

protected void service(HttpservletRequest req´╝îHttpservletResponse resp)
throws servletException´╝îIOException {
system.out.println("Service called...");

destroy method, which is executed when the Servlet instance is destroyed (proving that the Servlet instance is destroyed)

public void destroyO {
system.out.print1n("Instance destroyed...");}

The life cycle of servlets can be simply summarized into four steps: servlet class loading – > instantiation – > service – > destruction.

  1. The Web Client issues an Http request to the Servlet container (Tomcat)
  2. The Servlet container receives requests from the web Client
  3. The Servlet container creates an HttpServletRequest object to encapsulate the information requested by the Web Client into this object
  4. The Servlet container creates an HttpServletResponse object
  5. The Servlet container calls the HttpServlet object service method, takes Request and Response as parameters, and passes them to HttpServlet 6 HttpServlet calls relevant methods of HttpServletRequest object to obtain Http Request information

Specific code implementation

package com.liu.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ser02")
public class servlet02 extends HttpServlet {

    /**
     * Ready / service method
     * Used to process requests
     * System method, automatically called by the server
     * This method is called when a request arrives at ServLet
     * This method can be called multiple times
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet Called");
    }

    /**
     * Destruction method
     * System method, automatically called by the server
     * When the server shuts down, or the application stops
     * Execute once
     */
    @Override
    public void destroy() {
        System.out.println("servlet Destroyed");
    }

    /**
     * Initialization method, automatically called by the system
     * When the request reaches the ServLet container, the ServLet container will judge i whether the ervLet object exists. If it does not exist, create an instance and initialize it
     * This method is executed only once
     * @throws ServletException
     */
    @Override
    public void init() throws ServletException {
        System.out.println("servlet Was created");
    }
}

When opening the service and accessing the browser

When closing the service

Topics: Java Tomcat servlet