Sunday, 9 March 2014

Servlet Life Cycle

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
1. If an instance of servlet does not exist, the web container
  • Loads the servlet class.
  • Create instance of the servlet class.
  • Initializes the servlet instance by calling the init method.
2. Invoke the service method, passing a request and response object,
3. In the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method.
So Life cycle of servlet involves calling following methods from its creation till destruction
  1. init()
  2. service()
  3. destroy()

Initializing a Servlet

After the Web container loads and instantiates the servlet class and before it delivers requests from clients, the Web container initializes the servlet. You can customize this process to allow the servlet to read persistent configuration data, initialize resources, and perform any other one-time activities by overriding the init method of the Servlet interface. A servlet that cannot complete its initialization process should throw UnavailableException.
The init  method called only once per request.It is called when the servlet is first created.
The Servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify the servlet to be loaded while server started.
When a user invokes the servlet by calling URL of servlet, a single instance of each servlet gets created and handed off to doGet() or doPost().
Init method simple create or loads some data that will be used throughout the life cycle.
 public void init() throws ServletException {

// Initialization code ...

}

The service() Method

The service provided by a servlet is implemented in the service method of a GenericServlet, the doMethod methods (where Method can take the value Get, Delete, Options, Post, Put, Trace) of an HttpServlet, or any other protocol-specific methods defined by a class that implements the Servlet interface.

The general pattern for a service method is to extract information from the request, access external resources, and then populate the response based on that information.

For HTTP servlets, the correct procedure for populating the response is to first fill in the response headers, then retrieve an output stream from the response, and finally write any body content to the output stream. Response headers must always be set before a PrintWriter or ServletOutputStream is retrieved because the HTTP protocol expects to receive all headers before body content.

Each time the server receives a request for servlet, the server spawns a new thread and calls service. The service() methods check HTTP request type (Get, Delete, Options, Post, Put, Trace) and calls appropriate method (doGet, doDelete, doOptions, doPost, doPut, doTrace)

The service() Method

public void service(ServletRequest request, 
ServletResponse response)
throws ServletException, IOException{
}

The doGet() Method

A GET request from normal request for a URL or from and HTML form it will handled by doGet() method.In servlet by default doGet() method will called if no request type is specified.

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// your code
}

The doPost() Method

A POST request from an HTML form it will handled by doPost() method.

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// your code
}

The destroy() method

When a servlet container determines that a servlet should be removed from service (for example, when a container wants to reclaim memory resources, or when it is being shut down), it calls the destroy method of the Servlet interface. In this method, you release any resources the servlet is using and save any persistent state.

All of a servlet's service methods should be complete when a servlet is removed. The server tries to ensure this by calling the destroy method only after all service requests have returned, or after a server-specific grace period, whichever comes first. If your servlet has operations that take a long time to run (that is, operations that may run longer than the server's grace period), the operations could still be running when destroy is called. You must make sure that any threads still handling client requests complete.

public void destroy() {
// Finalization code...
}

0 comments

Post a Comment