Easy Tutorial
❮ Servlet Intro Servlet Environment Setup ❯

Servlet Lifecycle

The Servlet lifecycle can be defined as the entire process from creation to destruction. The following are the steps followed by a Servlet:

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 each user request. Therefore, it is used for one-time initialization, similar to the init method in an Applet.

The Servlet is created when the user first invokes the URL corresponding to the Servlet, but you can also specify that the Servlet be loaded when the server first starts.

When a user calls a Servlet, a new Servlet instance is created, and each user request generates a new thread, which is appropriately passed to the doGet or doPost method. The init() method simply creates or loads some data that will be used throughout the Servlet's lifecycle.

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 that performs the actual tasks. The Servlet container (i.e., the web server) calls the service() method to handle requests from the client (browser) and writes the formatted response back to the client.

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

Here is the signature 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 the appropriate methods like doGet, doPost, doPut, doDelete, etc., as needed. Therefore, you do not need to take any action for the service() method; you only need to override doGet() or doPost() based on the request type from the client.

The doGet() and doPost() methods are the most commonly used methods for each service request. Here are the signatures of these methods.

doGet() Method

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

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

doPost() Method

A POST request comes from an HTML form that specifically specifies the METHOD as POST, and it 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 the destroy() method is called, the Servlet object is marked for garbage collection. The definition of the destroy method is as follows:

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

Architecture Diagram

The diagram below shows a typical Servlet lifecycle scenario.

❮ Servlet Intro Servlet Environment Setup ❯