Servlet lifecycle


Published on

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Servlet lifecycle

  1. 1. 1. Explain about servlet lifecycle methods and theirsyntaxes with the help of a diagram?Servlets are managed components. They are managed by web container. Of the variousresponsibilities of web container, servlet life cycle management is the most importantone. A servlet is managed through a well defined life cycle that defines how it is loaded,instantiated ad initialized, handles requests from clients and how it is taken out of service.The servlet life cycle methods are defined in the javax.servlet.Servlet interface of theServlet API that all Servlets must implement directly or indirectly by extendingGenericServlet or HttpServlet abstract classes. Most of the servlet you develop willimplement it by extending HttpServlet class. Init and Destroy:Just like applets, servlets can define init() and destroy() methods. A servlets init(ServletConfig)method is called by the server immediately after the server constructs the servlets instance.Depending on the server and its configuration, this can be at any of these times: When the server starts When the servlet is first requested, just before the service() method is invoked At the request of the server administrator In any case, init() is guaranteed to be called before the servlet handles its first request.The servlet life cycle methods defined in Servlet interface are init(), service() anddestroy(). The life cycle starts when container instantiates the object of servlet class andcalls the init() method, and ends with the container calling the destroy() method.The syntaxes of this methods are shown below.1)public void init(ServletConfig config) throws ServletException or public void init()2)public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException3)public void destroy()The servlet life cycle consists of four steps, instantiation, initialization, request handlingand end of service.Loading and instantiationDuring this step, web container loads the servlet class and creates a new instance of theservlet. The container can create a servlet instance at container startup or it can delay it
  2. 2. until the servlet is needed to service a request.InitializationDuring initialization stage of the Servlet life cycle, the web container initializes theservlet instance by calling the init() method. The container passes an object implementingthe ServletConfig interface via the init() method. This configuration object allows theservlet to access name-value initialization parameters from the web applicationRequest handling service() methodAfter a servlet is properly initialized, the servlet container may use it to handle clientrequests. Requests are represented by request objects of type ServletRequest. The servletfills out response to requests by calling methods of a provided object of typeServletResponse. These objects are passed as parameters to the service method of theServlet interface. In the case of an HTTP request, the objects provided by the containerare of types HttpServletRequest and HttpServletResponse.End of service destroy() methodWhen servlet container determines that the servlet should be removed from the service, itcalls the destroy() method of the servlet to allow servlet to release any resources it isusing (eg. database connections or file handles). Before calling the destroy() method, thecontainer allows any request threads that are currently running in the service method tocomplete execution within a defined time limit. Once the servlet is removed out ofservice, container will not send any requests to the servlet. If the servlet needs to be put inservice again, the container will create a new servlet instance and the life cycle beginsfrom the initialization phase.
  3. 3. lifecycle of a servletExample:import*; import javax.servlet.*; import javax.servlet.http.*; public class InitCounter extends HttpServlet{ int count; public void init(ServletConfig config) throws ServletException { super.init(config); String initial = config.getInitParameter("initial"); try { count = Integer.parseInt(initial); } catch (NumberFormatException e) { count = 0; } } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); count++; out.println("Since loading (and with a possible initialization");
  4. 4. out.println("parameter figured in), this servlet has been accessed"); out.println(count + " times."); } } The init() method accepts an object that implements the ServletConfig interface. It uses theconfig objects getInitParameter() method to get the value for the init parameter named initial.This method takes the name of the parameter as a String and returns the value as a String. Thereis no way to get the value as any other type. This servlet therefore converts the String value to anint or, if theres a problem, defaults to a value of 0.