Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Servlet lifecycle


May 14, 2021 Servlet


Table of contents


Servlet lifecycle

The servlet lifecycle can be defined as the entire process from creation to destruction. Here's how Servlet follows:

  • Servlet initializes by calling the init() method.
  • Servlet calls the service() method to handle requests from clients.
  • The servlet terminates (ends) by calling the destroy() method.
  • Finally, the servlet is garbage collected by JVM's garbage collector.

Now let's discuss the lifecycle approach in more detail.

Init() method

The init method is designed to be called only once. I t is called the first time the servlet is created and is no longer called every time a user requests it. Therefore, it is used for one-time initialization, just like Applet's init method.

The servlet is created when the user first calls the URL that corresponds to the servlet, but you can also specify that the servlet is loaded the first time the server starts.

When a user calls a servlet, a servlet instance is created, and each user request generates a new thread that is handed over to the doGet or doPost method when appropriate. The init() method simply creates or loads data that will be used throughout the lifecycle of the servlet.

The init method is defined as follows:

public void init() throws ServletException {
  // 初始化代码...
}

Service() method

The service() method is the primary way to perform real-world tasks. The servlet container (that is, the Web server) calls the service() method to process requests from the client (browser) and writes the formatted response back to the client.

Each time the server receives a servlet request, the server generates a new thread and invokes the service. The service() method checks the TYPE of HTTP request (GET, POST, PUT, DELETE, etc.) and calls methods such as doGet, doPost, doPut, doDelete, etc. when appropriate.

Here are the characteristics of the method:

public void service(ServletRequest request, 
                    ServletResponse response) 
      throws ServletException, IOException{
}

The service() method is called by the container, and the service method calls doGet, doPost, doPut, doDelete, and so on when appropriate. Therefore, you don't have to do anything with the service() method, you just need to overload doGet() or doPost() based on the type of request from the client.

The doGet() and doPost() methods are the most commonly used methods for each service request. Here are the characteristics of both approaches.

DoGet() method

Get requests come from a normal request for a URL, or from an HTML form that does not specify METHOD, which is handled by the doGet() method.

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet 代码
}

DoPost() method

The POST request comes from an HTML form specifically designated ASD, which is handled by the doPost() method.

public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet 代码
}

Destroy() method

The destroy() method is called only once and at the end of the servlet life cycle. The destroy() method allows your servlet to shut down database connections, stop background threads, write cookie lists or click counters to disk, and perform other similar cleanup activities.

After the destroy() method is called, the servlet object is marked as garbage collected. The destroy method is defined as follows:

  public void destroy() {
    // 终止化代码...
  }

Schema diagram

The following illustration shows a typical servlet lifecycle scenario.

  • The first HTTP request to reach the server is delegated to the servlet container.
  • The servlet container loads the servlet before calling the service() method.
  • The servlet container then handles multiple requests generated by multiple threads, each executing a single servicelet instance of the service() method.

Servlet lifecycle

Here's a simple example of the lifecycle:

//servlet生命周期,的三个方法,1.被创建,执行且只执行一次init方法,2.提供服务,执行service方法,执行多次 3.被销毁,当Servlet服务器正常关闭时,执行destroy方法,只执行一次。
     
     @Override
     public void init() throws ServletException {
         // TODO Auto-generated method stub
         super.init();
     }
     
     @Override
     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         // TODO Auto-generated method stub
         super.service(req, resp);
     }
     
     @Override
     public void destroy() {
         // TODO Auto-generated method stub
         super.destroy();
     }