Servlet and Servlet Life cycle


Servlet is Server side technology which is use to devlope web Application. and  Handle the client Request and Response .

Lifecycle of Servlet:

When we start the server container will be start,at container start up some task  happens-
  1. Container creates thread pool....
  2. Container reads web.xml file using SAX..SAX Parser(simple API for XML) which is mainly use to read XML document....
  3. Servlet Context Object will be created and initialize with context parameter.
  4. Container checks weather...any  servlet class will be loaded and   instantiate by calling default constructor.
  5. init() method will be invoke in servlet instance by passing Config  as a parameter
        All the filter specified will be init..
        All the listener specified will be init...
Now container is ready to take any incoming request.
Web server takes the request and handed over to container then container takes the in requested URI and checks weather any servlet is configured  in web.xml whose <url-pattern> is same as incoming request URI.

  • If no matching is found then error message will be given to the client with the status 404.
  • If matching found then container takes the servlet name and cheaks the status- it has already initialize or not.
  •  If not initilze then it will repate the same step.If initialize then
  •  container takes the servlet instance and one thread from thread pool
  • assigned request processing to the thread by handing over to servlet instance.
  1. Thread start the request processing by doing some tasks.     ServletRequest Objcet will be created and Initilze with requset param,req header
  2. ServletResponse objcet will be created and ini with response header,stream etc.
  • service() method will be call by passing req and res as a parameter.
  • Once service method execution is complite stream will flush to client request and response object will be destroy thread will return to thread pool.


Post a Comment