• Loading
    • Servlet Lifecycle

      Learning Objectives
      After completing this session, you will be able to:
      • Describe the lifecycle of a servlet


      Servlet Lifecycle

      Servlet lifecycle consists of the following steps:
      • Server loads the servlet class
      • Servlet Instantiation
      • Servlet Initialization
      • Servlet handles either zero or more client requests
      • The server removes the servlet


      Servlet Initialization
      • When a server loads a servlet, the server runs the init() method of the servlet.
      • Initialization completes before client requests are handled and before the servlet is destroyed.
      • Server calls the init() method only once in the life- cycle of a servlet.
      • The server can not reload a servlet until and unless the server has destroyed the servlet by calling the destroy() method.


      Interacting with Clients

      • The server is able to handle the client requests after initialization.
      • The Service() method of servlet is called, which in turn calls the doXXX() methods like doGet() or doPost().
      • When extending javax.servelt.http.HttpServlet you most commonly override the doGet() and/or doPost() methods.


      Destroying a Servlet

      • Servlets run until and unless the server destroys them, for example, at the request of System Administrator.
      • When a server destroys a servlet, then the server runs the destroy() method of the servlet.
      • The method is run once and the server will not run that servlet again until and unless the server reloads and reinitializes the servlet.


      Try It Out
      Problem Statement: Describe the lifecycle of a servlet.

      Code:
      Code:
      public class LifeCycle extends HttpServlet { 
      private ServletConfig config; 
      private StringBuffer sb; 
      public void init(ServletConfig config) throws ServletException { 
      sb=new StringBuffer(); 
      sb.append("in init(config) method of HttpServlet->"); 
      this.config=config; 
      init(); 
      } 
      public void init(){ 
      sb.append("in init method of HttpServlet->"); 
      } 
      public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{ 
      sb.append("in service method of HttpServlet->"); 
      doGet(req,res); 
      } 
      public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 
      sb.append("in doGet method of HttpServlet->"); 
      res.setContentType("text/html"); 
      res.getWriter().println("<html><body><h1>"+sb.toString()+"</h1></body></html>"); 
      sb.delete(0, sb.length()); 
      } 
      public void destroy(){ 
      System.out.println("destroyed"); 
      } 
      )
      How It Works:
      • When a server loads a servlet, the server runs the init() method of the servlet.
      • Initialization completes before client requests are handled and before the servlet is destroyed.
      • Server calls the init() method only once in the life- cycle of a servlet.
      • The server can not reload a servlet until and unless the server has destroyed the servlet by calling the destroy() method.



      Summary

      1) Servlet lifecycle consists of the following steps:
      • Server loads servlet class
      • Instantiates the servlet
      • Initializes the servlet
      • Servlet handles zero or more client requests
      • The server removes the servlet


      2) When a server loads a servlet, then the server runs the init() method of the servlet.

      3) After initialization, the servlet is able to handle client requests.

      4) The servlets run until and unless the server destroys them, for example, at the request of System Administrator..


      Test your Understanding


      1. Explain the lifecycle of servlet.
      2. State the reason for the discrepancy in the output displayed when the application was first loaded and the browser was refreshed



    Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.