Explain the life cycle of Servlet, i.e. Instantiation, Initialization, Service, Destroy, Unavailable

Explain the life cycle of Servlet, i.e. Instantiation, Initialization, Service, Destroy, Unavailable.

- The life cycle is managed by the servlet container in which the servlet is deployed,

Life Cycle of a Servlets:

1. Instantiation:

- At the time of starting the web container, it searches for the deployment descriptor (web.xml) for every web application.
- Once the servlet is found in the deployment descriptor, the container creates an instance of the Servlet class, which is considered as the loading of the servlet class.

2. Initialization:

- The init() method of GenericServlet class is invoked by the HttpServlet class which is the sub class of the GenericServlet, meaning that the HttpServlet inherits the init() method of GenericServlet.

- The init() method performs some specific actions like a constructor of a class which initializes an instance at the start up.

- It is automatically be invoked by the servlet container.

- This action causes the parsing by the application context (web.xml).

- The init() method is overloaded with zero parameters and with a parameter that takes a ServletConfig parameter.

- The init() method is invoked only once when the servlet is first loaded.

3. Service:

- The operations of a servlet is performed by the service () method.

- This method has HttpServletRequest and HttpServletResponse parameters.

- The service () method is invoked by the container and is called for each request processed.

- By default the method returns / dispatches the requests to the appropriate methods, usually a developer’s overridden helper methods such as doGet() and doPost().

- You need to author the code to delegate to the helper methods or invoke super. service() method.

4. Destroy:

- The destroy method is invoked when the container is shutting down or if the requests are not sent for a while for the servlet, then the destroy() method may be invoked to ensure the resources are not available for the servlets that are being requested.

- The destroy() method invoked only once before the unloading the servlet.
Explain the purpose of Servlet interface
The javax.servlet.HttpServlet / javax.servlet.Servlet is the interface that is to be implemented by all the servlets....
Explain the underlying method of Servlet interface
The init() method, The service() method, The destroy() method, The getServletConfig(), The getServletInfo() method......
What is GenericServlet class?
This class implements Servlet and ServletConfig interfaces. This class can be extended by a servlet.....
Post your comment