Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Servlet Life Cycle

servlet life cycle

  1. Loading Servlet Class : A Servlet class is loaded when first request for the servlet is received by the Web Container.
  2. Servlet instance creation :After the Servlet class is loaded, Web Container creates the instance of it. Servlet instance is created only once in the life cycle.
  3. Call to the init() method : init() method is called by the Web Container on servlet instance to initialize the servlet.

    Signature of init() method :

    
    public void init(ServletConfig config) throws ServletException
    
  4. Call to the service() method : The containers call the service() method each time the request for servlet is received. The service() method will then call the doGet() or doPost() methos based ont eh type of the HTTP request, as explained in previous lessons.

    Signature of service() method :

    
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    
  5. Call to destroy() method: The Web Container call the destroy() method before removing servlet instance, giving it a chance for cleanup activity.

load on startup in web.xml

In Java, a servlet is not loaded by default. When a servlet container gets a request then only a servlet is loaded. First time when a servlet is loaded, It takes more time as compared to the next time. If you want to avoid the delay the access time then <load-on-startup> tag can be used in the web.xml file. This tag forces the servlet container to load the servlet fast when a server is started.


Below is an example of how to use the <load-on-startup> tag.

Web.xml

	
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <servlet>
  	<servlet-name>abc</servlet-name>
  	<servlet-class>com.app.studytonight.AddServlet</servlet-class>
	<load-on-startup>1</load-on-startup> 
  </servlet>
</web-app>
	

Below is an example of how to sequence multiple servlet loading using the <load-on-startup> tag.

Web.xml

	
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <servlet>
  	<servlet-name>abc1</servlet-name>
  	<servlet-class>com.app.studytonight.AddServlet1</servlet-class>
	<load-on-startup>1</load-on-startup> 
  </servlet>
  <servlet>
  	<servlet-name>abc2</servlet-name>
  	<servlet-class>com.app.studytonight.AddServlet2</servlet-class>
	<load-on-startup>2</load-on-startup> 
  </servlet>
  <servlet>
  	<servlet-name>abc3</servlet-name>
  	<servlet-class>com.app.studytonight.AddServlet3</servlet-class>
	<load-on-startup>3</load-on-startup> 
  </servlet>
  <servlet>
  	<servlet-name>abc4</servlet-name>
  	<servlet-class>com.app.studytonight.AddServlet4</servlet-class>
	<load-on-startup>-1</load-on-startup> 
  </servlet>
</web-app>
	

In the above example, com.app.studytonight.AddServlet1, com.app.studytonight.AddServlet2, com.app.studytonight.AddServlet3 will be loaded as soon as the server is started in the assigned sequence i.e 1, 2, and 3. But com.app.studytonight.AddServlet4 will not be loaded when the server is started because it has a negative value. The negative value will be loaded according to the container preference.