The Life Cycle of a Servlet
This tutorial explains the Servlet interface and Servlet life cycle. A servlet example is given at the end which demonstrates the life cycle of a servlet.
Servlets are managed components. They are managed by web container. Of the various responsibilities of web container, servlet life cycle management is the most important one. A servlet is managed through a well defined life cycle that defines how it is loaded, instantiated ad initialized, handles requests from clients and how it is taken out of service. 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.
The signature of this methods are shown below.
public void init(ServletConfig config) throws ServletException
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()
The servlet life cycle consists of four steps, instantiation, initialization, request handling and end of service. Each of these steps is explained below.
Loading and instantiation
During this step, web container loads the servlet class and creates a new instance of the servlet. The container can create a servlet instance at container startup or it can delay it until the servlet is needed to service a request.
Initialization
During initialization stage of the Servlet life cycle, the web container initializes the servlet instance by calling the init() method. The container passes an object implementing the ServletConfig interface via the init() method. This configuration object allows the servlet to access name-value initialization parameters from the web application’s deployment descriptor (web.xml) file. The container guarantees that the init() method will be called before the service() method is called.
The init() method is typically used to perform servlet initialization, creating or loading objects that are used by the servlet in the handling of its requests.
The init() method is commonly used to perform one time activity. One of the most common use of init() method is to setup the database connection or connection pool.
Request handling
After a servlet is properly initialized, it is ready to handle the client requests. If the container has a request for the servlet, it calls the servlet instance’s service() method. The request and response information is wrapped in ServletRequest and ServletResponse objects respectively, which are then passed to the servlet's service() method. In the case of an HTTP request, the objects provided by the container are of types HttpServletRequest and HttpServletResponse.
Service() method is responsible for processing the incoming requests and generating the response.
End of service
When the servlet container determines that a servlet should be removed from service, it calls the destroy () method of the Servlet instance to allow the servlet to release any resources it is using. The servlet container can destroy a servlet because it wants to conserve some memory or server itself is shutting down. Before the servlet container calls the destroy() method, it allows any threads that are currently running in the service method of the servlet to complete execution, or exceed a server defined time limit. Once the destroy() method has completed, the container will release the servlet instance for garbage collection. If it needs another instance of the servlet to process requests it creates the new instance of the servlet and life cycle starts again.
Destroy() method is used to release any resources it is using. The most common use of destroy() method is to close the database connections.
Shortly I will put an example servlet which demonstrates various stages of servlet life cycle.
