Servlet life cycle (code + diagram)

After reading some articles on the Internet, I feel that it is too theoretical. Now let's briefly talk about the life cycle of Servlet, and then verify it with code.

When the browser accesses the server, the server parses the url. If the browser does not want static resources, the server accesses the corresponding Servlet according to the URI. Taking tomcat as an example, tomcat will first judge whether the Servlet has been instantiated, because the Servlet is single instance by default, that is, it will only be created once (wait for the code demonstration), If it is not created, it means this is the first access. Then tomcat will create the instance object of the corresponding Servlet, put it into a container similar to HashMap for management, call the init method of the Servlet, and then call the service method of the Servlet. If it is not the first time, tomcat will call the service method directly. Finally, when tomcat is closed, the Servlet will be destroyed, but the destroy method will be called before destruction. The following is a demonstration with figure and code.

The following shows the process followed in the Servlet life cycle

  • After Servlet initialization, the init () method is called.
  • The Servlet calls the service() method to process the client's request.
  • The destroy() method is called before Servlet destroys.
  • Finally, the Servlet is garbage collected by the JVM's garbage collector.

The figure is roughly

Let's first verify that the Servlet mentioned above is a singleton, that is, it will only be created once. The test code is as follows

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/single")
public class SingleServlet extends HttpServlet {

    private int count = 0;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("current servlet The number of visits is:" + ++count);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

We define a private attribute count in the code to record the number of accesses. If the output of each access is 1, then our Servlet is not singleton. If the value of the output count plus 1, then it is singleton. We start the server to access it and see the console output.

We visited four times and found that the number of visits increased each time. We also verified that the Servlet is a singleton. Since it is a singleton, we can be sure that this class will only be initialized once. Because the init method will only be called when it is created, init will only be called once. Similarly, destroy is the same. We still design a test to illustrate. The code is as follows

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/life")
public class LifeServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("Called init method");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Called service method");
    }

    @Override
    public void destroy() {
        System.out.println("Called destroy method");
    }
}

We start the server and make our first visit

Sure enough, for the first time, we first create the Servlet instance object, then call the init method, then call the service method, and we continue to visit the second time.

         
As we thought, because the instance object of the Servlet already exists, we can call the service method directly. Let's visit it several times and try it.

 
It was found that only the service method was called. Finally, we close tomcat.

 
Sure enough, this is also the same as I think. Servlet was destroyed when the server was shut down, and the destroy method was called before destruction.

So far, our Servlet life cycle has been demonstrated. After the demonstration, we must have a deeper understanding of the Servlet life cycle. Finally, we give an architecture diagram of the Servlet life cycle on w3c to deepen our understanding of the Servlet life cycle again.

The above is only personal opinion. If there is any error, please point it out in the comment, and I will modify it when I see it. If you think this article is helpful to you, then point a praise and support it!!!

Keywords: Java Tomcat Back-end servlet server

Added by ginoitalo on Thu, 24 Feb 2022 17:38:24 +0200