Servlet Lifecycle , Servlet Lifecycle Methods
The servlet lifecycle is controlled and maintained by the container in which the servlet has been deployed.The behavior of the lifecycle,methods are explained below.
Servlet Lifecycle Process :
Loading ( Instantiation ) :
The servlet can load in two ways. 1. load-on-startup and 2. On first client request.
load-on-startup : Programmer need to include the tag (<load-on-startup> 1 </load-on-startup>) in the web.xml .In this case the servlet container creates the servlet class object during the deployment of the web application or during the server startup.
On first client request :If the servlet get first request from the client, servlet container loads the servlet class from WEB-INF\classes folder of the deployed web application and instantiates the class using Zero argument constructor. (Example : Class.forName(“ExampleServlet”), newlnstance() ;).
Initialization /init(-) :
Once the servlet instantiation process success,immediately the servlet container initializes the servlet object by calling the Servlet.init(ServletConfig) method.
It is one time execution block of the servlet program. Servlet container calls this method automatically when it instantiate the servlet class.
Implementation /Service (-,-) :
In this state, the servlet container creates the ServletRequest and ServletResponse objects, after that the Container calls Servlet.service(ServletRequest,ServletResponse) method every time, when it receives request from the client to servlet program.
Programmer generally places the request processing and response generation logics in this method. It is the most essential and important life cycle method of servlet program.
Destruction /destroy() :
When the servlet container is about to destroy the servlet class object , it will call the destroy() method.This method is also a one time execution block like init(-).
In this method ,programmer generally places un-initialization logic like closing JDBC connection object.
|It is not recommended to implement the service method with synchronization techniques, because it reduces the performance of the servlet container.|
|Related Posts :|