Life Cycle of a Servlet (Servlet Life Cycle)
|
The web container maintains the life cycle of a servlet instance. Let’s see the life cycle of the servlet:
|
|
As displayed in the above diagram, there are three states of a servlet: new, ready and end. The servlet is in new state if servlet instance is created. After invoking the init() method, Servlet comes in the ready state. In the ready state, servlet performs all the tasks. When the web container invokes the destroy() method, it shifts to the end state. |
1) Servlet class is loaded
|
The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container. |
2) Servlet instance is created
|
The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle. |
3) init method is invoked
|
The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below: |
public void init(ServletConfig config) throws ServletException
4) service method is invoked
|
The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below: |
public void service(ServletRequest req, ServletResponse res)throws ServletException,IOException
5) destroy method is invoked
|
The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the Servlet interface is given below: |
public void destroy()
How Servlet works?
It is important to learn how servlet works for understanding the servlet well. Here, we are going to get the internal detail about the first servlet program.
The server checks if the servlet is requested for the first time.
If yes, web container does the following tasks:
- loads the servlet class.
- instantiates the servlet class.
- calls the init method passing the ServletConfig object
else
- calls the service method passing request and response objects
The web container calls the destroy method when it needs to remove the servlet such as at time of stopping server or undeploying the project.
How web container handles the servlet request?
The web container is responsible to handle the request. Let’s see how it handles the request.
- maps the request with the servlet in the web.xml file.
- creates request and response objects for this request
- calls the service method on the thread
- The public service method internally calls the protected service method
- The protected service method calls the doGet method depending on the type of request.
- The doGet method generates the response and it is passed to the client.
- After sending the response, the web container deletes the request and response objects. The thread is contained in the thread pool or deleted depends on the server implementation.