Wednesday, November 4, 2009

Servlet Life Cycle



Servlets are container managed. The life and death of servlets is under the control of the Servlet Engine. Servlet engine is developed strictly according to the Servelt specification. Our servlet also is written aonforming the same specification and by following the Servlet API. Therefore the servlet container can manage the servlet life cycle. The servlet life cycle steps describe the process by which the servlet container loads, instantiates, unloads and invokes its life cycle methods.

Life Cycle:
  1. The servlet container loads the servlet class that we developed and deployed into it. This can happen either at the time of web application startup or on receiving the client request for the first time.
  2. The servlet engine creates the servlet instances.
  3. Servlet engine creates the ServletConfig object.
  4. Servlet engines create the init method by suppyling ServletConfig object as argument.
  5. Servlet engine creates ServletRequest object & ServletResponse objects.
  6. Servlet engine calls the public service method and supplies the above two objects as arguments.
  7. Within the public service method these two objects are converted into Http specific objects and public service method calls protected service method.
  8. Within the protected service method client request type is calibrated (GET or POST). Depending upon the type of request, protected service method calls either doGet() or doPost(). In one of these methods a servlet developers implements the clinet request processing clode that involves.
    i) Evaluating the HttpServletRequest object and capture the user input.
    ii) Producing dynamic data by communicating with the database.
    iii) Building dynamic web content and sending it to the client using HttpServletResponse object.
  9. If the container receives another client request, process starts from strp 5.
  10. When the container is instructed to keep the servlet out of service, the servlet engine calls the destroy method on the servlet instance just before the servlet instance is garbage collected.


Bookmark and Share

1 comments:

tea said...

When a client request is made, the service method is called and passed a request and response object. The servlet first determines whether the request is a GET or POST operation. It then calls one of the following methods: doGet or doPost. The doGet method is called if the request is GET, and doPost is called if the request is POST. Both doGet and doPost take request (HttpServletRequest) and response (HttpServletResponse).

Post a Comment

Type here your comments