Saturday, February 14, 2009

Servlet LifeCycle / Life Cycle of a Servlet

Servlet: Servlets are server side java components which provide a powerful mechanism for developing server side java programs. Servlets are mainly used to develop Web-based applications.

Servlet lifecycle

Servlet lifecycle is handled by the servlet container. Servlets are managed components and are managed by web(servlet) container. Servlet life cycle management is the most important responsibility of web container. 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.

Syntax:

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:

1. Instantiation
2. Initialization(init())
3. request handling (service())
4. end of service (destroy())

Instantiation (Loading the Servlet)


During this step, web(servlet) 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(servlet) 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 that is it will be called only once in the life time of the servlet. 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. The service phase of the Servlet life cycle represents all interactions with requests until the Servlet is destroyed. The Servlet interface matches the service phase of the Servlet life cycle to the service() method. The service() method of a Servlet is invoked once per a request and is responsible for generating the response to that request.


The Servlet specification defines the service() method to take two parameters: a javax.servlet.ServletRequest and a javax.servlet.ServletResponse object. These two objects represent a client's request for the dynamic resource and the Servlet's response to the client.

By default a Servlet is multi-threaded, meaning that typically only one instance of a Servlet1 is loaded by a JSP container at any given time. Initialization is done once, and each request after that is handled concurrently2 by threads executing the Servlet's service() method.

End of service() method


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. The destruction phase of the Servlet life cycle represents when a Servlet is being removed from use by a container. The Servlet interface defines the destroy() method to correspond to the destruction life cycle phase. Each time a Servlet is about to be removed from use, a container calls the destroy() method.

No comments: