Saturday, March 7, 2020

Servlet Life Cycle

Servlet Life Cycle

The Servlet interface defines three methods to instantiate a servlet, to receive and respond to client requests, and to finalize a servlet instance and the resources held by this instance. There are five methods provided by the Servlet Interface. The init(), service() and destroy() are known as the life cycle methods of the Servlet. These are invoked by the web container itself. The other two methods are getServletConfig() and getServletInfo().


This method initializes the servlet. It is invoked automatically by the Servlet container when the request is accepted to load the servlet.

service(ServletRequet, ServletResponse)

This method handles a single request. This method works upon the request-response paradigm. The request object is used to receive the request and the response object is used to respond with the results to the client.


This method clears all the resources are being like memory, file handles, and threads. This method ensures that any persistent state is synchronized with the servlet's current in-memory state. The destroy() method is executed, the Servlet container releases all the references of this Servlet instance so that it becomes eligible for garbage collection.


This method returns a ServletConfig object, carrying any initialization parameters and servlet configuration.


This method returns a string carrying the data related to the servlet, such as its author, version, etc.

The Servlet Life Cycle

The lifecycle of a servlet is managed by the Servlet container in which the servlet has been deployed. The web container performs the following steps upon the mapping received for a Servlet,
  1. If an instance of the servlet does not exist, the container
    • Loads the servlet class.
    • Creates a new object of the Servlet class.
    • Initializes the servlet object by calling the init() method.
  2. Else call the service(ServletRequest, ServletResponse) method
  3. If the servlet object is not required, the container will destroy the servlet object by invoking the destroy() method.

Servlet Life cycle programminghunk



import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet("/LifeCycleDemo")//a simple servlet implemnting Servlet
public class LifeCycleDemo implements Servlet {
     private static final long serialVersionUID = 1L;
     ServletConfig config=null;
     public LifeCycleDemo() {//constructor

     public void destroy() {//destroy called to remove the
                          //object from the memory
           System.out.println("Servlet Destroyed");

     public ServletConfig getServletConfig() {//Not a life-cycle method
           return config;

     public String getServletInfo() {//Not a life-cycle method
           return "this is test servlet";

     @Override// initialize the servlet
     public void init(ServletConfig config) throws ServletException {
           System.out.println("Servlet initialized");

     @Override// the service method
     public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
            System.out.println("inside service method");

             PrintWriter out = resp.getWriter();
             out.print("<b> Servlet Life Cycle methods Example</b>");