java4all@1986 java. Powered by Blogger.

Life Cycle of Servlet?

>> Wednesday, June 1, 2011

Life Cycle of Servlet:




When we sent a request from client to server, the protocol will establish a socket connection Between Client and Server.Then the protocol will carry the client request to server machine.At server machine there are Main Server and Container.First the Main Server will Validates the client request ,if it is successfully then it will bypass to Container,Then the Container will perform the following steps.
1.)The container will get the requested Uri with Application name and resource name .It checks for the resources name i.e whether it is html/jsp ,if it is html/jsp then the Container will get requested Resource above WEB-INF which is in public area.
2.)If the requested resource is not html/jsp ,then it will think that is an url-pattern which is available under WEB-INF.
3.)Then the container will go to Web.XML and find the respective resources i.e Servlet by matching the URL-pattern,then the  Container will reform the following steps      

  1. Loading and Inatantiation: The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the attribute <load-on-startup> of web.xml file. If the attribute <load-on-startup> has a positive value then the servlet is load with loading of the container otherwise it load when the first request comes for service. After loading of the servlet, the container creates the instances of the servlet.
  2. Initialization: After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to the init() method. The init() must be called by the servlet container before the servlet can service any request. The initialization parameters persist untill the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet.

    The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet.
  3. Servicing the Request: After successfully completing the initialization process, the servlet will be available for service. Servlet creates seperate threads for each request. The sevlet container calls the service() method for servicing any request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object.
  4. Destroying the Servlet: If the servlet is no longer needed for servicing any request, the servlet container calls the destroy() method . Like the init() method this method is also called only once throughout the life cycle of the servlet. Calling the destroy() method indicates to the servlet container not to sent the any request for service and the servlet  releases all the resources associated with it. Java Virtual Machine claims for the memory associated with the resources for garbage collection.

                

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP