Servlet Life Cycle Tutorial with Methods

Servlet Life Cycle Tutorial with Methods

This Servlet Life Cycle Tutorial gives the meaning of life cycle, the methods involved, their importance and how and where to use in coding

1. What is Servlet Life Cycle?

Definition: Different states in which a Servlet exists between its object creation and object garbage collection is known as life cycle of Servlet.

2. Who is responsible to maintain the life cycle? Is it the Developer?

Everything goes smooth with Servlets without much Developer’s intervention. Servlet Container or Servlet Engine is responsible for:

  1. To provide services required for the execution of Servlet
  2. Maintaining the life cycle of the Servlet
  3. Calling callback methods (of life cycle) at appropriate times for the smooth Servlet execution
  4. Servlet object creation and garbage collection (destruction) is entirely looked after by container

3. How to get a Servlet container?

Just load a Web server like Tomcat or Weblogic or WebSphere. These Web servers get you the Servlet container.

4. Any methods exist in Servlet Life Cycle like init(), start() and paint() as in the case of Applet life cycle?

Yes. The life cycle comprises of 3 methods and all are defied in javax.servlet.Servlet interface and overridden by HttpServlet. The methods are

  1. init() method
  2. service() method
  3. destroy() method

In it’s life cycle, the Servlet should exist in one of these three methods. These methods are callback methods called automatically by Servlet container. Following gives the explanation on these methods elaborately.

1. The init() method

When the client request a Servlet (by giving alias name declared in web.xml), the container loads the Servlet into the RAM.  To start execution, it creates the Servlet object and calls init() method.  At this stage, the container creates another object of ServletConfig interface representing the Servlet and passes it to the parameter of init().  For this reason, the parameter of init() method is an object of ServletConfig.   For every Servlet under execution in the container, there exists a corresponding ServletConfig object.

Let us what Java API says about this method

  • void init(ServletConfig config) throws ServletException: Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

This method is called and executed only once in the life cycle of Servlet (like init() of Applet).


  1. As one object and one process exists for all the clients (when all clients call the same servlet. Note, different servlets will have different servlet objects), Developer should not use static variables in Servlet (ofcourse, not advised in any server-side component).
  2. To the maximum extent, try to avoid using threads in a Server-side component, as the Developer threads may clash with the container threads and thereby the process may not move exactly as expected.

2. The service() method

After completing the execution of init(), the container calls service() method to process the client request. It is the heart of the Servlet like run() method for a thread. All the code the Servlet should execute to honour the client request, should be written in this method. Receiving request with client data, generating response and sending to client – all this happens here only.

Following is the method signature as defined in javax.servlet.Servlet interface

  • void service(ServletRequest req, ServletResponse res) throws ServletException, Called by the servlet container to allow the servlet to respond to a request. This method is only called after the servlet’s init() method has completed successfully.

At this point, let us go a little deeper. When the Servlet is in the RAM honouring the client request, if multiple clients request the same Servlet, then how many objects of Servlet are created? Servlets support multithreading (here, where Servlet differs from CGI/PERL). For all the clients, only one Servlet object is created (for this reason, init() is called only once) and for each client a separate service() method is created and called. That is, each client is treated as a separate thread (spawns a new thread) and each thread launches a separate service() method.

Note: Incase of HttpServlet (not GenericServlet), the service() method can be replaced by more appropriate doGet() or doPost() methods.

3. The destroy() method

When the service() method execution is over (the execution is over means, when the client request is fully honoured by sending all the required response), the Servlet process is terminated with the calling of destroy() method. In the destroy() method, the Servlet object is garbage collected by JVM and is the end of the life cycle.

Following is the method signature

  1. void destroy(): Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet’s service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

The container does any clean up action and frees any resources held by the Servlet like memory, file handles and threads etc.

Servlet Life Cycle Tutorial

Figurative explanation

Servlet Life Cycle Tutorial

  1. First time Client 1 calls the Servlet. init() is called and service() is created for the Client 1.
  2. Same servlet is called by Client 2 and Client 3. For them new two threads of Thread2 and Thread 3 are spawned and defines two separate service() methods.
  3. When all the three threads are executed and no further threads are pending execution, the container destroys the Servlet object after calling destroy() method.

Leave a Reply

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