Tuesday, 15 July 2014

Load on Startup in web.xml

The load-on-startup element of web-app loads the servlet at the time of deployment or server start if value is positive. It is also known as pre initialization of servlet.
You can pass positive and negative value for the servlet.

Advantage of load-on-startup element

As you know well, servlet is loaded at first request. That means it consumes more time at first request. If you specify the load-on-startup in web.xml, servlet will be loaded at project deployment time or server start. So, it will take less time for responding to first request.

Passing positive value

If you pass the positive value, the lower integer value servlet will be loaded before the higher integer value servlet. In other words, container loads the servlets in ascending integer value. The 0 value will be loaded first then 1, 2, 3 and so on.
Let's try to understand it by the example given below:
web.xml
  1. <web-app>  
  2.  ....  
  3.   
  4.   <servlet>  
  5.    <servlet-name>servlet1</servlet-name>  
  6.    <servlet-class>com.javatpoint.FirstServlet</servlet-class>  
  7.    <load-on-startup>0</load-on-startup>  
  8.   </servlet>  
  9.   
  10.   <servlet>  
  11.    <servlet-name>servlet2</servlet-name>  
  12.    <servlet-class>com.javatpoint.SecondServlet</servlet-class>  
  13.    <load-on-startup>1</load-on-startup>  
  14.   </servlet>  
  15.   
  16.  ...  
  17. </web-app>  
There are defined 2 servlets, both servlets will be loaded at the time of project deployment or server start. But, servlet1 will be loaded first then servlet2.

Passing negative value

If you pass the negative value, servlet will be loaded at request time, at first request.

No comments:

Post a Comment