Servlet life cycle Methods:
- Servlet engine controls the life cycle of servlet
- Servlet life cycle is described by three life cycle methods under 5 life cycle phases
- life cycle methods are
- init(ServletConfig obj)
- service(servletRequest, servletResponse)
instance and hence the name.
Life cycle phases :
- Does not exist phase
- Instantiation phase
- Initialization phase
- Servicing phase
- Destruction phase
- Servlet class is made available to the servlet engine but not yet its instance is created.
- It is known as doesn't exist phase of a servlet.
- The process of creating the object of a class is known as instantiation.
- Servlet engine loads user user defined servlet class file from secondary memory into
primary memory dynamically
class c= class.forName("name");
- Servlet engine creates the instance of loaded servlet class.
- Servlet engine uses the following piece of code to load the servlet clas dynamically to instantiate it.
Class c= class.forName("Servlet class name");
- Servlet engine creates servletConfig object
- Servlet engine calls the init() method on the servlet instance by supplying servletconfig object
- Once init method completely executed servlet is ready to serve the client request.
- Servlet engine creates servlet request and servlet response object based on the web server
provided client information.
- Servlet engine calls service method on the servlet instance by supplying two object reference as arguments
- Once service method is completely executed , client request is served and request-response cycle is complete.
- Within the service method request object is used to capture the user input
- Response object is used to build the dynamic page and hand over the same to the web server
- Destruction phase of servlet represents servlet being removed from use by container