Servlet life cycle

If you are all set to attend an interview related to J2EE/Java Web application development then, it is recommended to brush up this important concept.I witnessed this question in telephonic round interview. Today I am documenting it for future reference and for others.
What is Servlet ?:  A Servlet is a Java program that runs within a servlet container. Servlets receive and respond to requests from Web clients. Servlets could in principle communicate over any client–server protocol, but they are most often used with the HTTP protocol. Thus "servlet" is often used as shorthand for "HTTP servlet". As every object is having a life-cycle so as of servlet instance.
The life-cycle of a servlet is controlled by the container in which the servlet has been deployed. Life cycle of servlet can be broadly divided into three stages :
1. Initialization stage -  init() -   Executed only once
2. Service stage -            service() -     For each request
3. Destroy stage. -          destroy() -     Executed only once
 We can represent all three method executing in servlet container as follows :
                                             Diagram taken from :

1. Initialization phase : In this stage web-container initializes the servlet instance by calling the init() method and ONE instance per JVM for each servlet is created. It is important to note that init() method can be invoked in either of two ways :
1. On demand basis  - On arrival of first HTTP request for the given servlet .
2. On Servlet container start: When servlet container start,it reads the web.xml ,finds the declared servlets in the classpath and if <load-on-startup> is configured with an  integer value 0 or more then for the given servlet one instance of that servlet will be created and are stored in memory and reused every time the request arrives. (Always remember REUSED !!).Sample code for servlet config of  load-on-startup:
          //Mapping details .....
          <load-on-startup> 1 </load-on-startup>
Note : load-on-startup can specify an (optional) integer value. If the value is greater than 0, it indicates an order for servlets to be loaded, servlets with higher numbers get loaded after servlets with lower numbers.
Please note, init(ServletConfig) is being called by the servlet container to indicate a servlet that the servlet is being placed into service.We can override this init() method see this for reference. 
  public void init(ServletConfig config) throws ServletException 
       //some initialization - like getting database connection,
       // resource management etc.
Here is a visual explanation of Instantiation phase, in layman terminology.
Notes : 
  • init() is not the entry point of a servlet. servlet constructor is executed before execution of init() but it is not  recommended to use constructor for any Servlet. why? Find here
  • The container would decide how many instances of servlet would be instantiated upfront to cater the requests.This is container implementation..(Confused !! But it's fact)
2. Service phase : In this stage for every new request a new thread is created or allocated from a pool to invoke that servlet instance which was created in earlier stage . The HttpRequest and HttpResponse objects will be new for each new request.
One commonly asked question in interview : How HTTP request coming from web client is served by servlet ? 
On request arrival web container(servlet container) calls service() method of servlet and 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 response object. Lets consider following code blocks:
 public class SimpleHttpServlet extends HttpServlet {
  protected void doGet( HttpServletRequest request,
                        HttpServletResponse response)
        throws ServletException, IOException {
      response.getWriter().write("<html><body>GET response</body></html>");
  protected void doPost( HttpServletRequest request,
                         HttpServletResponse response)
      throws ServletException, IOException {
      response.getWriter().write("GET/POST response");
HttpServlet(javax.servlet.http.HttpServle) class reads the HTTP request(coming from client), and determines if the request is an HTTP GET, POST, PUT, DELETE, HEAD etc. and calls one the corresponding method.
Notes :
  • It is important to note that each request is served by a new thread if and only if our servlet is not implementing SingleThreadModel interface. It is not recommended to use SingleThreadModel interface. 
  • Is servlet threadsafe ? Answer is : No, but we can make by it thread-safe by following some standard so that it can serve multiple request in thread safe manner. How we can make servlet thread safe?
3. Destroy phase : When a servlet is unloaded by the servlet container, its destroy() method is called. It is executed only once in servlet life cycle. A servlet is unloaded by the container if the container shuts down, or if the container reloads the whole web application at run-time.

Refer J2EE developer interview questions

======================End of article=====================


  1. Replies
    1. Great Article android based projects

      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. It is a great post. Keep sharing such kind of useful information.


  3. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    PHP Training in Chennai
    PHP Course in Chennai
    AWS Training in Chennai
    Blue Prism Training in Chennai
    CCNA Course in Chennai
    Cloud Computing Training in Chennai
    Data Science Course in Chennai
    PHP Training in Porur

  4. This post is really useful and helpful to know more about the things which you have shared. I appreciate you for such a great amount of information. I assure this would be beneficial for many people. Best Advanced Java & Data Structures Course In Delhi

  5. Informative blog! I am truly awed by this blog post! Very clear explanation about the full stack Development. I agree with all points.Hire Full Stack Developers

  6. Hello, This is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information……nice…….
    Visit website: ""

Previous Post Next Post