Introduction To Servlet Life Cycle-1
Introduction To Servlet Life Cycle-1
What is a servlet?
A servlet is a Java technology based web component, managed by a container, that generates dynamic content. A servlet can be considered as a tiny Java program which processes user request and generates dynamic content. There are many other alternatives like php, asp .net or CGI, for developing dynamic web sites. Benefit of using servlets over other technologies is servlets are developed in Java, so it comes with all benefits of Java language and it is platform independent. Following are the some advantages of using servlets. (1) They are generally much faster than CGI scripts. (2) They use a standard API that is supported by many web servers. (3) They have all the advantages of the Java programming language, including ease of development and platform independence. (4) They can access the large set of APIs available for the Java platform.
Sr l t e ve
Gn rc e ve e ei S r l t
Hp e ve t Sr l t t
Sr l t e us e veRq e t
Hp e veRq e t t Sr l t e us t
S r l t e p ne e veRs o s j v x e ve.* a a .s r l t
4
Hp e veRs o s t S r l t e p ne t j v x e ve.ht .* a a .s r l t t p
Invokes the service method, passing a request and response object. If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method.
Loading and Instantiation: 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. 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.
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.
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.
Things to remember?
init Executed once when the servlet is first loaded. Not called for each request. Service Called in a new thread by server for each request. Dispatches to doGet, doPost, etc. Do not override this method! doGet, doPost, doBlah Handles GET, POST, etc. requests. Override these to provide desired behavior. destroy Called when server deletes servlet instance. Not called after each request.