Life Cycle of Servlet

The servlet life cycle is controlled by containers.The container perform the following activities, when a request is happened in a servlet.

1.If  the servlet instance does not exist, the Web container

  • Loads the servlet class.
  • The servlet class creates an instance.
  • Initializes the servlet instances by calling the init () method.

2.Invokes the service () method by passing the request and response object attributes.

Finalization process happens when the container needs to remove the servlet. The destroy () helps to finalizes the servlet.

The servlet life cycle can be categorized into  four parts

Loading and  Instantiation:

During the startup stage or when the first request is sent, the servlet container loads.  Servlets are managed by servlet containers. For example tomcat. The load activity looks on the servlet attribute <load-on-startup>. If the attribute value is positive, then the servlet is loaded else when the first request comes for service it loads. After the loading prosess is complete the container produces servlet instances.


After producing the instances or servlet can service any request, the init () must be called by the servlet container. The initialization parameters remains till the servlet is destroyed. If the loading process is successfully completed, the servlet will be available else the container drops the servlet.

Servicing the Request:

The servlet container calls the service () method only after the initialization process is complete. For each request is happening, the servlet creates different threads. Once we call the service () method it determines which type of request we called. That means we identify the request type is doGet () type or doPost () type. After identify the type of request we are handling the request and sends response to the client by using the response object method.

Destroying the Servlet:

The servlet container calls the destroy () method, when the servlet is no longer used for any service request. Like init () method the destroy () method is called or requested only once throughout the servlet life cycle.  Once we request the destroy () method it unplug all the services. That is the servlet container do not sent any request for service and the container releases all the actions or resources associated with it. Java Virtual Machine (JVM) claims for the memory associated with the resources for garbage collection.

The servlet life cycle methods are defined in the javax.servlet.Servlet interface of the servlet API that all servlets must implement directly or indirectly by extending GenericServlet or HttpServlet abstract classes. Most of the servlet you develop will implement it by extending HttpServlet class.

The servlet life cycle methods defined in Servlet interface are init (), service () and destroy (). The life cycle starts when container instantiates the object of servlet class and calls the init () method, and ends with the container calling the destroy () method.

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletLifeCycleExample extends HttpServlet {
private int count;
public void init (ServletConfig config) throws ServletException {
getServletContext().log("init() called");
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 getServletContext().log("service() called");
 response.getWriter().write("Incrementig the count: Count = "+count);

 public void destroy() {
	getServletContext().log("destroy() called");

Manipulate  Servlet Life-Cycle Events

By defining listener objects we can monitor and respond to event’s in a servlet’s life cycle. By using these listener objects, first we define the listener class and specify the listener class.

Object Event Listener Interface and Event Class
Web context Initialization and destruction




Attribute added, removed, or replaced

javax.servlet.ServletContextAttributeListener and


Session Creation, invalidation, and timeout




Attribute added, removed, or replaced

javax.servlet.http.HttpSessionAttributeListener and


Eg: The listeners.ContextListner  class  creates and removes the database helper and counter objects used in the Bookstore application. The methods retrieve the Web context object from ServletContextEvent and then store (and remove) the objects as servlet context attributes.

import database.BookDB;
import javax.servlet.*;
import util.Counter;
public final class ContextListener
implements ServletContextListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
try {
BookDB bookDB = new BookDB();
context.setAttribute("bookDB", bookDB);
} catch (Exception ex) {
"Couldn't create database: " + ex.getMessage());
Counter counter = new Counter();
context.setAttribute("hitCounter", counter);
context.log("Created hitCounter" +
counter = new Counter();
context.setAttribute("orderCounter", counter);
context.log("Created orderCounter" +
public void contextDestroyed(ServletContextEvent event) {
context = event.getServletContext();
BookDB bookDB = context.getAttribute(