Servlet Life cycle
Last updated
Last updated
Following are the steps that happens in the initialization sequence of the servlet class.
Container searches the servlet class under J2EE compliant folder structure, WEB-INF/classes
.
It loads the class using Class.forname(FQCN)
obtained from the web.xml
servlet tag.
To create instance of the servlet class, the web-container uses default constructor.
Once the servlet instance is created by the container, WC injects servlet config object and makes it available to init()
method.
Now, as web container uses thread pool (Executor) mechanism to serve client request, which is created during web container startup time, web container will simply pool out one thread from this thread pool. This thread's run method invoked by WC, will call public service() -> protected service() of the HttpServlet class.
From the service method, do****() is called as per the request, and client request is processed.
At the end of servlet life cycle, destroy method is invoked, after which servlet instance will be marked for garbage collection.