Friday, 3 February 2012

J2EE: SERVLET: Servlet Life Cycle

  Life cycle of a servlet starts when its first load in the memory space in web server and ends when unloaded from the server memory. we can divide this in 3 steps.


STEP 1: INITIALIZATION:
  • Server loads the servlet class.
  • Creates an instance for the servlet class.
  • Server initializes the instance by calling init() method. 


STEP 2: SERVICE:
  • Server creates a request & a respond object based on the client's request.
  • Invokes the service() method of the servlet, passing the request respond object.
  • service() method process the request and use the respond object to create the client respond.
  • service() method can invoke can also invoke other methods like doGet(), doPost(), doDelete(), doOptions(), doPut() and doTrace().

STEP 3: DESTROY:
  • Server calls the destroy() method to relinquish any resources such as file handles that are allocated to that servlet. destroy() method is invoked once in a servlet's life time.
  • Then the server unload the servlet and its objects, and it's memory can be garbage collected.

    Describes the Servlet Life Cycle within Web container or Web Server

                       *Unavailable exception thrown       *Initialization failed

    4 comments:

    1. Thanks for visiting! :)

      ReplyDelete
    2. Thanks for sharing the information i found it very useful.

      Thanks again..

      ReplyDelete
      Replies
      1. @namastu

        you r most welcome my friend!!

        Delete

    ShareThis