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 :

Servlet Lifecycle Loading ( Instantiation ) :

The servlet can load in two ways. 1. load-on-startup and 2. On first client request.

Servlet Lifecycle  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.

Servlet Lifecycle  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() ;).


Servlet Lifecycle Initialization /init(-) :

Servlet Lifecycle Once the servlet instantiation process success,immediately  the servlet container initializes the servlet object  by calling the Servlet.init(ServletConfig) method.
Servlet Lifecycle It is one time execution block of the servlet program. Servlet container calls this method automatically when it instantiate the servlet class.
Servlet Lifecycle Implementation /Service (-,-) :

Servlet Lifecycle 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.

Servlet Lifecycle 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.


Servlet Lifecycle Destruction /destroy() :

Servlet Lifecycle 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(-).

Servlet Lifecycle In this method ,programmer generally places un-initialization logic  like closing JDBC connection object.


Servlet LifecycleIt is not recommended to implement the service method with synchronization techniques, because it reduces the performance of the  servlet container.

Related Posts :
   [will not be published]

^ <