Sunday, March 16, 2008

The servlet life cycle

before learning about servlets lets discuss about the life cycle of the servlet.
when a client request something the request is directed to the container.the container makes two objects,which are
  • request object
  • response object
and it binds these objects to a thread.
the thread comes in to action in one of the two ways mentioned below.
  • a thread pool provides one of the threads it have
  • a new thread is created(new Thread();)
then this thread handles everything after this.it makes a servlet object and using that object calls the init() method of the servlet.this is done only once. after initiating the servlet the container calls the service() method of the servlet using our thread created.
then the service() method redirect the request to a relevant method which may be doGet() or doPost() according to the request. we will learn about these methods in future.
after executing one of these methods the generated response is directed to the browser by the container.and the threads work is also over.

so the thread goes to dead state if it is a newly created one and it will go back to pool if it was taken form the thread pool.
now there is no need of the request and response objects.so they are eligible for the garbage collector to be collected.

and when the servlet is no longer needed the destroy() method of the servlet is called and the servlet moves to the dead state.this is also called only once.

**for every request we make a new thread comes to the action.**

No comments: