Thursday, 31 March 2011

J2ee: The Life Cycle of a Servlet

The life cycle of a Java servlet is a very simple object-oriented design. A servlet is constructed and initialized. It then services zero or more requests until the service that it extends shuts down. At this point the servlet is destroyed and garbage collected. This design explains why servlets are such a good replacement for CGI. The servlet is loaded only once and it stays resident in memory while servicing requests.
The interface that declares this framework is the javax.servlet.Servlet interface. The Servlet interface defines the life cycle methods. These methods are init(), service(), and destroy(). But before the init() call, a without argument constructor is invoked.

init()
The init() method is where the servlet's life begins. It is called by the server immediately after the servlet is instantiated. It is called only once. In the init() method, the servlet creates and initializes any resources, including data members, that it will be using while handling requests. The init() method's signature is defined as follows:

  • public void init(ServletConfig config) throws ServletException;
The init() method takes a ServletConfig object as a parameter. You should save this object so that it can be referenced later. The most common way of doing this is to have the init() method call super.init(), passing it the ServletConfig object.
The init() method can throw a ServletException. If, for some reason, the servlet cannot initialize the resources necessary to handle requests, the init() method will throw a ServletException.

service()
The service() method handles all requests sent by a client. It cannot start servicing requests until the init() method has been executed. You will not usually implement this method directly, unless you extend the GenericServlet abstract class.
The most common implementation of the service() method is in the HttpServlet class. The HttpServlet class implements the Servlet interface by extending GenericServlet. Its service() method supports standard HTTP/1.1 requests by determining the request type and calling the appropriate method. The signature of the service() method is as follows:

  • public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;
The service() method implements a request and response paradigm. The ServletRequest object contains information about the service request, encapsulating information provided by the client. The ServletResponse object contains the information returned to the client.

destroy()
This method signifies the end of a servlet's life. When a service is being shut down, it calls the servlet's destroy() method. This is where any resources that were created in the init() method will be cleaned up. If you have an open database connection, you should close it here. This is also a good place to save any persistent information that will be used the next time the servlet is loaded. The signature of the destroy() is as follows:

  • public void destroy();

No comments:

Post a Comment