8 Servlet life cycle 1

Servlet life cycle

The life cycle of Servlet is managed by the container and goes through four stages:

Phase Times Timing

Create 1 time First request

Initialize 1 time after instantiation

Execute the service multiple times per request

Destroy 1 time Stop service

When the client browser requests the Servlet for the first time, the container will instantiate the Servlet, then call the init method once, and execute the service method in a new thread to process the request. After the service method is executed, the container will not destroy the Servlet but do cache processing. When the client browser requests the Servlet again, the container will directly find the Servlet object from the cache and execute the Service method again in a new thread . When the container calls the destroy method once before destroying the Servlet.

In Servlet, we generally don't use member variables lightly!!!! It may cause thread safety problems

If you want to use it, you should try to avoid modifying member variables

If we want to make changes, we should pay attention to thread safety issues

If we add thread-safe encoding processing ourselves, it will seriously affect efficiency

To sum up: In principle, if you can use member variables, you don’t need them!!!

Servlet code

package com.mashibing.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 * @Author: Ma HaiYang
 * @Description: MircoMessage:Mark_7001
public class MyServlet4 extends HttpServlet {
    // Member variables
    public MyServlet4()   {// Method to construct a Servlet object
        System.out.println("MyServlet4 Constructor invoked");
        try {
        } catch (InterruptedException e) {
    public void init() throws ServletException {// initialization
        System.out.println("MyServlet4 init invoked");
        try {
        } catch (InterruptedException e) {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // executive service
        System.out.println("MyServlet4 service invoked");
    public void destroy() {// destroy
        System.out.println("MyServlet4 destory invoked");

Configuring Servlet s


The above conclusion can be confirmed by requesting the servlet multiple times and viewing the console output. It is worth noting that if the Servlet needs to be instantiated and initialized when the service starts, we can add load-on-startup to configure the startup sequence in the servlet configuration. The configured number is the startup sequence, conflicts should be avoided and should be >6

The process of Servlet processing requests

When the browser requests us to create a Servlet based on the get method, the doGet method in our custom Servlet will be executed. The reason why the doGet method can be executed and process the get request is that the container will parse the web.xml file in the WEB-INF directory of the web project when it starts. In this file, we configure the binding between the Servlet and the URI. The container passes the The resolution of the request can obtain the URI of the requested resource, and then find the Servlet bound to the URI and instantiate it (note: only instantiate once, if the Servlet can be found in the cache, it will not be instantiated again) . When instantiating, the Servlet interface type is used as the definition of the reference type, and the init method is called once. Since the method is rewritten in HttpServlet, the init method in HttpServlet is finally executed (the Init method in HttpServlet is an empty method body. ), and then call the service method in the new thread. Since the Service method is rewritten in HttpServlet, the service method in HttpServlet is finally executed. In the service method, get the request method through request.getMethod() to judge. If it is a Get method request, execute the doGet method. If it is a POST request, execute the doPost method. If it is submitted based on GET, and the doGet method in HttpServlet is rewritten in our custom Servlet, then the doGet method in our custom Servlet will eventually be executed according to the polymorphic characteristics of Java.

Tags: Java Front-end servlet

Posted by nickthegreek on Wed, 15 Mar 2023 04:12:50 +0530