The life cycle of a servlet is managed by a web container.
Servlet life cycle steps:
- Load Servlet Class.
- Create Servlet instance.
- Call init() method.
- Call service() method.
- Call destroy () method.
1. Load Servlet Class: The web container loads the servlet when the first request is received. This step is executed only once at the time of the first request.
2. Create Servlet instance: After loading the Servlet class, the web container creates the Servlet instance. Only one instance is created for a servlet and all concurrent requests are executed on the same servlet instance.
3. Call init() method: After creating the servlet instance, the web container calls the servlet’s init method. This method is used to initialize the servlet before processing the first request. It is called only once by the web container.
4. Call service() method: After the initialization process web container calls the service method. The service method is called for every request. For every request, the servlet creates a separate thread.
5. Call destroy () method: This method is called by the web container before removing the servlet instance. Destroy method asks the servlet to release all the resources associated with it. It is called only once by the web container when all servlet threads have exited or in a timeout case.