Explain page life cycle of JSP Application

Mumbai University > Information Technology > Sem 4 > Web Programming

Marks: 10M

Year: May 2014

1 Answer
  • In life cycle of Java Servlet page (JSP) , there are 3 important methods:
    • init()
    • service()
    • destroy()
  • When a user enters the url in the the web browser and makes a request, the browser generates the HTTP request and sends it to the web server. Web Server maps this request to the corresponding servlet.

enter image description here

The init( ) method:

  • It request for a servlet received by the servlet engine, checks to see if the servlet is already loaded.
  • If not, uses a class loader to get the required servlet class and instantiates it by calling the constructor method.
  • After the servlet is loaded, but before it services any requests, the init ( ) method is called.
  • Inside init( ), the resources used by the servlet are initialize like establishing database connection.
  • This method is called only once just before the servlet is placed into service.
  • The init( ) method takes a ServletConfig object as a parameter. Its signature is:
  • public void init(ServletConfig config) throws ServletException

The service( ) method:

  • The service( ) method handles all requests sent by a client.
  • It cannot start servicing requests until the init( ) method has been executed.
  • Only a single instance of the servlet is created and the servlet engine dispatches each request in a single thread.
  • The service( ) method is used only when GenericServlet class is extended.
  • Since servlets are designed to operate in the HTTP environment, the HttpServlet class is extended.
  • The service(HttpServletRequest, HttpServletResponse) method examines the request and calls the appropriate doGet() or doPost() method.
  • A typical Http servlet includes overrides to one or more of these subsidiary methods rather than an override to service().

The destroy( ) method:

  • This method signifies the end of a servlet’s life. This method can be invoked by Servlet engine only.
  • The resources allocated during init( ) are released.
  • It saves persistent information that will be used the next time the servlet isloaded.
  • The servlet engine unloads the servlet.
Please log in to add an answer.