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)
- destroy()
instance and hence the name.
Life cycle phases :
- Does not exist phase
- Instantiation phase
- Initialization phase
- Servicing phase
- Destruction phase
Doesn't Exist:
- 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.
Instatiation:
- 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");
c.newInstance(); - 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");
c.newInstance();
Initialization:
- Servlet engine creates servletConfig object
- Servlet engine calls the init() method on the servlet instance by supplying servletconfig object
as argument. - Once init method completely executed servlet is ready to serve the client request.
Servicing :
- 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:
- Destruction phase of servlet represents servlet being removed from use by container
No comments