Servlet Lifecycle

The lifecycle of a servlet is managed by the servlet container. The life cycle can be summarized in 5 stages:
  1. Load Servlet class
  2. Create Servlet instance
  3. Call method init()
  4. Call the method service()
  5. Call the method destroy()
Steps 1, 2, and 3 are executed only once, when the servlet is loaded. By default, the servlet is not loaded until the first request is received. You can force the servlet container to load the serlvet when it is running.
Step 4 is executed multiple times each time  Request  HTTP.
Step 5 is executed when the servlet container destroys the servlet.

This is an algorithm that represents the lifecycle of a servlet:

servlet life cycle

The steps are explained in detail below:

Load the Servlet class

First, for the servlet to be called, The servlet container must load the Servlet class. This is done like any other class.

Create Servlet instance

Now that the class is loaded, the container creates a new servlet instance. Typically, only one instance is created and the Request  are processed in the same servlet instance. It is at the servlet container to decide but, in general, there is only one instance.

Call init()

The method init() is designed to be called only once. When the instance is created, the init() is called. . It allows the servlet to initialize before the first request is processed. You can specify the servlet initialization parameters in the web.xml.

The init() method looks like this:

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

Call service()

For each request received, the server creates a new thread and executes the methodservice(). The method service() checks the type of the HTTP request (GET, POST, PUT, DELETE, etc.) and calls the corresponding methods doGet, doPost, doPut, doDelete, etc. As long as the servlet remains active, the method service() can be invoked again. So, this step may be the longest step in the lifecycle of a servlet since it runs multiple times.

Here is the declaration of this method:

public void service(ServletRequest request, 
ServletResponse response)
throws ServletException, IOException{
}
The methods doGet() and doPost() are frequently used every time they arrive. request. Here is the statement  of both methods.

The doGet()

The GET request is created in a URL or in an HTML form that has no method defined and must be processed by the doGet().

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

The doPost()

La  method query  POST is created in an HTML form and should be processed by the doPost().

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

Call destroy()

method < span style="font-family: Courier New, Courier, minivan;" >destroy() is called only once at the end of a servlet's lifecycle. This method allows you to close the connection, stop threads, save cookie lists.
After destory() is called, the servlet object is destroyed. The method destory() looks like this:

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