Tuesday 15 July 2014

Servlet Life Cycle

Servlet container controls the Servlet life cycle. When a request is mapped to a servlet.

1. Servlet container checks, whether the instance of this servlet is already loaded or not. If instance is not loaded then,
      a. Loads the Servlet class
      b. Create an instance of the servlet class
      c. Initialize the servlet by calling the servlet init method.

 Note : 
   a. During initialization, the servlet instance can throw an UnavailableException or a ServletException. In this case, the servlet must not be placed into active service and must be released by the servlet container. The destroy method is not called as it is considered unsuccessful initialization.

 b. A new instance may be instantiated and initialized by the container after a failed initialization. The exception to this rule is when an UnavailableException indicates a minimum time of unavailability, and the container must wait for the period to pass before creating and initializing a new servlet instance.

2. Once initialization done, then Invokes the service method, passing a request and response object.

3. If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method. When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state.

Before the servlet container calls the destroy method, it allow any threads that are currently running in the service method of the servlet to complete execution or exceed a server defined time limit. After destroy method completes, the servlet instance is eligible for Garbage Collection.   





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment