English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Servlet Lifecycle

The Servlet lifecycle can be defined as the entire process from creation to destruction. The following is the process followed by Servlets:

  • is called after the Servlet is initialized init () method.

  • Servlet call service() method to handle client requests.

  • is called before the Servlet is destroyed  destroy() method.

  • Finally, Servlets are garbage collected by the JVM's garbage collector.

Now let's discuss the lifecycle methods in detail.

init() method

The init method is designed to be called only once. It is called when the Servlet is first created and is not called again for subsequent user requests. Therefore, it is used for one-time initialization, similar to the init method of an Applet.

A Servlet is created when the user first calls the URL corresponding to the Servlet, but you can also specify that the Servlet is loaded when the server starts up.

When a user calls a Servlet, a Servlet instance is created, and a new thread is generated for each user request, which is appropriately handed over to the doGet or doPost method. The init() method simply creates or loads some data, which will be used throughout the life cycle of the Servlet.

The definition of the init method is as follows:

public void init() throws ServletException {
  // Initialization code...
}

service() method

The service() method is the main method for executing actual tasks. The Servlet container (i.e., the Web server) calls the service() method to handle requests from the client (browser) and write the formatted response back to the client.

Each time the server receives a Servlet request, it generates a new thread and calls the service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. at the appropriate time.

Below are the characteristics of this 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, etc. at the appropriate time. Therefore, you do not need to do anything with the service() method; you just need to overwrite doGet() or doPost() according to the request type from the client.

doGet() and doPost() methods are the most commonly used methods in each service request. Below are the characteristics of these two methods.

doGet() method

A GET request comes from a normal request of a URL or from an HTML form that does not specify a METHOD, which is handled by the doGet() method.

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

doPost() method

POST requests come from an HTML form that is specially specified with METHOD set to POST, which is handled by the doPost() method.

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

destroy() method

The destroy() method is called only once, at the end of the Servlet lifecycle. The destroy() method allows your Servlet to close database connections, stop background threads, write the Cookie list or click counter to disk, and perform other similar cleanup activities.

After calling the destroy() method, the servlet object is marked for garbage collection. The destroy method is defined as follows:

  public void destroy() {
    // Termination code...
  }

Architecture Diagram

The following figure shows a typical Servlet lifecycle diagram.

  • 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.

  • Then the Servlet container handles multiple requests generated by multiple threads, with each thread executing a single Servlet instance's service() method.