Life Cycle of the Servlet.

Hello Friends,

This is one of my tutorials on Servlet. As we all know, servlets are managed by the web container and among the various responsibilities of the container, life cycle of servlet is the most important one. The life cycle defines that how the servlet is loaded, instantiated and initialized, how it handles requests from clients and how it is taken out of service. The servlet’s life cycle methods are defined in the javax.servlet.Servlet interface which is implemented by every Servlet class by extending the abstract classes – “GenericServlet or HttpServlet”.

The servlet life cycle consists of mainly three methods init(), service() and destroy(). The life cycle starts when container instantiates the object of servlet class and calls the init() method, and ends when the container calls destroy() method. The service() method is the one that actually services the client’s request and processes it.

Following are the four main stages in the life cycle of the servlet:-

1) Loading and Instantiation: The container loads the servlet during startup or when the first client request is made. The loading of the servlet depends on the attribute <load-on-startup> of web.xml file. If the attribute <load-on-startup> has a positive value then the servlet is loaded with loading of the container otherwise it loads when the first request comes for service. After loading of the servlet, container creates the instances of that servlet.

2) Initialization: After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to it. The init() must be called by the servlet container before the servlet can service any request so that the servlet object is created. Note that, there are not multiple instances for one servlet. The initialization parameters persist till the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet.

The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet.

3) Servicing the Request: Only after the initialization process, the servlet will be ready for service. Servlet creates separate threads for each request, but still there is only a single instance for each servlet. The container calls the service() method for servicing the request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object.

4) Destroying the Servlet: If the servlet is no longer needed for servicing any request, the servlet container calls the destroy() method . Like as init(), this method is also called only once throughout the life cycle of the servlet. Calling the destroy() method indicates to the container not to sent any request for service and hence the servlet releases all the resources associated with it.

Note : There is only one main state in life cycle of servlet i.e. Initialized. If the servlet isn’t initialized then it is being initialized by running the constructor or init() method, or it is being destroyed by calling destroy() method.

Below is the pictorial representation of the flow or stages of the servlet through which it has to pass throughout its life.

Hope this will help you.

Thanks,

admin@code2java.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.