The Servlet Life Cycle and Methods


To run servlet, container calls the servlet lifecycle.


The servlet lifecycle has following methods:


The Servlet Life Cycle Methods:
The javax.servlet.Servlet interface defines the life cycle methods of the servlet such as init(), service() and destroy().
The Web container invokes the init() , service(), and destroy() methods of a servlet during its life cycle.

The sequence in which the Web container calls the life cycle methods of a servlet is as follows :

1) The Web container loads the servlet class and creates instance of the servlet class.
2 The Web container invokes the init() method of the servlet instance during initialization of the Servlet.
The init() method is invoked only once in the servlet life cycle .
3) The Web container invokes the service() method to allow a servlet to process a client request.
4) The service() method processes the request and returns the response back to the Web container.
5) The servlet then waits to receive and process subsequent requests as explained in steps 3 and 4.
6) The Web container calls the destroy() method before removing the servlet instance from the service.
The destroy() method is also invoked only once in the a servlet life cycle.


The init() method


1. The init() method is called during the initialization phase of the servlet life cycle.
2. The Web container first maps the requested URL to the corresponding servlet available in the Web container and then instantiates the servlet.
3. The Web container then creates an object of the ServletConfig interface, which contains the startup configuration information, such as initialization parameters of the servlet.
4. The Web container then calls the init() method of the servlet and passes the ServletConfig object to it.
5. The init() method throws a ServletException if the Web container cannot initialize the servlet resources.
6. The servlet initialization completes before any client requests are accepted.
7. The following code snippet shows the init() method:
public void init(ServletConfig config) throws ServletException


The service() Method

1. The service() method processes the client requests.
2. Each time the Web container receives a client request, it invokes the service() method.
3. The service() method is invoked only after the initialization of the servlet is complete.
4. When the Web container calls the service() method, it passes an object of the ServletRequest interface and an object of the ServletResponse interface.
5. The ServletRequest object contains information about the service request made by the client.
6. The ServletResponse object contains the information returned by the servlet to the client.


The following code snippet shows the service() method:

public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException

1. The service() method dispatches a client request to one of the request handler methods of the HttpServlet interface ,such as the doGet(), doPost(), doHead() or doPut().
2. The request handler methods accepts the objects of the HttpServletRequest and HttpServletResponse as parameters from the service() method.
3. service() method is not overridden in the HttpServlet as the Web container automatically invokes the service() method.
4. The servlet functionality of the HTTP Servlets is written in doGet() or doPost() methods.


The destroy() method

1. The destroy() method marks the end of the life cycle of a servlet.
2. The Web container calls the destroy() method before removing a servlet instance from the service.
3. The Web container calls the destroy() method when:
   a. The time period specified for the servlet has elapsed. The time period of a servlet is the period for which the servlet is         kept in the active state by the Web container to service the client request.
   b. The Web container needs to release servlet instances to conserve memory.
   c. The Web container is about to shut down.
4. In the destroy() method you can write the code to release the resources occupied by the servlet.

Summary

1. The classes and interfaces, which are used to develop a servlet, are packages in the javax.servlet and javax.servlet.http packages of the Servlet API.
2. The javax.servlet.Servlet interface defines methods that are used to manage the servlet life cycle.
3. The javax.servlet.ServletConfig interface is implemented by a Web container to pass configuration information to the servlet.
4. The HttpServletRequest object represents a request sent by a client using HTTP.
5. The HttpServletResponse object represents a response sent by a servlet to a client using HTTP.