Servlet Life cycle

This tutorials explains the life cycle of a servlet. The life cycle is managed by a servlet container where we deploy our web application.


  1. Load Servlet
    Container receives a request to load a servlet. If instance of the servlet is not present then the container loads the servlet class.
  2. Instance of Servlet
    Instance of servlet is created by the servlet container after loading the servlet class.
  3. Initialize the Servlet
    Container initializes the instance of the servlet by calling init method. Servlet is initialized only once. This lifecycle method is useful when you want to initialize certain things. init method is called only once when the instance of servlet is created. So if instance of a servlet is present in a container then for each request the service method is called directly and the setup task is not repeated for each request.
        public void init() throws ServletException{
  4. Service method is invoked
    After initializing the servlet, the container calls the service method. Service method checks the request type and calls the corresponding request methods. When no method is specified then the GET method is invoked.
  5. Destroy Servlet
    Destroy() method is called whenever a container is about to remove the instance of the servlet. In this method servlet can perform cleanup operations.