Lifecycle Of Servlet

What is a servlet?

A servlet, is a server component where 'serv' means server and 'let' means component.  A servlet is a java file, which takes a request from the client, processes the request, and provides an HTML page to that client.  If you don't have basic knowledge of servlet, you can visit my article to get an Introduction to Servlet
Let us now move towards the lifecycle of the servlet.

Servlet Interface

In servlet, we have five methods,
  1. getServletConfig()
  2. getServletInfo() 
  3. init()
  4. service()
  5. destroy() 
In these five methods, the bottom three are lifecycle methods.

Lifecycle of servlet

In its lifecycle, a servlet has three main methods -
1) init
2) service
3) destroy
Here, service() is the main method of a servlet.  Whenever a user calls any of the methods, the servlet service() is called.
lifecycle of servlet


When a web browser calls the servlet, init() is invoked.  It is called, only once, after the instance is created.  In init(), the compiler-supplied no-argument constructor runs and it must be completed before service() starts.  We can override init() to perform any initialization task, as mentioned below.
  1. public void init() throws ServletException {}  
We can perform any initialization task, create a database connection, or open a file that will be needed for the servlet.


Service() handles any request from the web browser.  Service() will figure out which method, i.e., either doGet() or doPost(), to call based upon the request HTTP method (Get, Post). service() is called, again and again, when a user requests for the method.  When service() completes its task, either the thread dies, or returns to the thread pool, which is managed by the container.  We can not override service(), but we can override doGet() and doPost().  See the below code.
  1. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {}  
  2. public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {}    
Based upon the user HTTP request, service() decides which method to override.  doGet() and/or doPost() are the main methods of a servlet, which perform the actual task. We call methods from doGet() and doPost().  When a user calls the method, a new thread is created for each new request, no matter if you are the same user, or a new user.


destroy() is called only once at the end of the servlet lifecycle.  We can perform any cleanup code before the servlet is killed, such as - closing database connection, freeing thread and cookies, closing file, etc.  To perform any cleanup task, we can override this method as below.
  1. public void destroy() {}  

Packages of servlet

In Java, we have two types of packages for the servlet.
  1. javax.servlet
  2. javax.servlet.http
lifecycle of servlet
That's it!  I hope this article is helpful to beginners, especially to those who are trying to build a career, in Java programming.