Ads Sponsored By Google :)

Wednesday, 16 January 2013

Explain servlet life cycle diagram in java|j2ee methods example ppt|pdf


servlet life cycle servlet life cycle diagram servlet life cycle in java servlet life cycle methods java servlet life cycle life cycle of a servlet servlet life cycle example java servlet life cycle diagram explain servlet life cycle in java servlet life cycle in j2ee

    Life Cycle Of  Servlet:

The life cycle of a servlet can be categorized into four parts: 

   1.Loading and Instantiation of Servlet:    

         The servlet webcontainer loads the servlet when startup or when the first request 
         is made.  The loading of    the servlet depends on the tags <load-on-startup> 
        </load-on-startup> of  web.xml file. If the attribute <load-on-startup></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  Or Object of the servlet. 

    2) Initialization Of Servlet: 

            After Completion of creating the instances Of servlets in webcontainer, the servlet 
       webcontainer calls the init() method  and it intializes the parameters in init() Method.
       But init() method Called Only once in the life cycle of servlet.it would available for 
       service when servlet loaded sucessfully.

  3) Servicing the Request:  
  
             After completing the initialization process of servlet, the servlet will available for 
        service. Servlet creates seperate threads for each request. The servlet webcontainer
        calls the service() method for servicing any request. The service() method finds the 
        kind of request that is arived to server and calls the appropriate method (doGet() or   
       doPost() in HttpServlet Class) for handling the request and sends response to the 
       client using the methods of the response object by using threads.

   Note: Thread is a process  in servlets .

  4.Destroying the Servlet:  
  
             The servlet is not exists for long time for servicing any request, the servlet
      webcontainer calls the destroy() method . Like the init() method destroy() method
     of servlet lifecycle is also called only once . Calling the destroy() method signifies 
     that servlet webcontainer not to recieve any request for service and the servlet  releases
    all the resources associated with it.


       Method Of Servlet LifeCycle:

               There are 3 methods in Servlet Life Cycle They Are:

           1)init()
           2)Service()
           3)Destroy()

       1)init():
                      This method in servlet  is used to intialize parameters.this method is called only 
        once in servlet lifecycle.


       2)Service():  
                                        This method in servlet is used to provide service to the request, and it 
      execute according  Business Logic that is in service method given by   
      developer.this method executes for request.
                   
       3)Destroy(): 
                    This method in servlet to close connection that open in the init() method.
        this method executes only once throughout lifecycle of servlet.



     For Example:    

                     import javax.servlet.*;
                     import java.io.*;

                     public LifeServlet extends GenericServlet
                      {
                          public void init()
                            {
                               //it executes only once
                               //Connection or intiliazation of parameters
                             }  
                      
                         public void service(req,res)
                           {
                             //it executes for every request 
                            //inside it Business Logic
                            }
                
                        public void destroy()
                       {
                        //it executes only Once 
                       //Closing or termination of Connection
                        }
                    }

1 comment:

LinkWithin