-->

8. load-on-startup in web.xml

Welcome File List: Previous                                                      Next: Request Dispatcher Interface

Load-on-startup in web.xml

load-on-startup:

The load-on-startup is the sub attribute of servlet attribute in web.xml. It is used to control when the web server loads the servlet. As we discussed that servlet is loaded at the time of first request. In this case response time is increased for first request. If load-on-startup is specified for a servlet in web.xml then this servlet will be loaded when the server starts. So the response time will not increase for fist request.
Any positive or negative value can be passed in the load-on-startup. If positive value is passed then all servlets having load-on-startup sub attribute will be loaded when server starts but a servlet having low positive value will be loaded first. In case of negative value servlet will be loaded at the time of first request.

Sample code of load-on-startup in web.xml.

<web-app>
 
   //other attributes  
 
   <servlet>  
  <servlet-name>servlet1</servlet-name>  
   <servlet-class>com.javawithease.business.Servlet1 </servlet-class>
    <load-on-startup>0</load-on-startup>  
   </servlet>  
 
  <servlet>  
    <servlet-name>servlet2</servlet-name>  
    <servlet-class> com.javawithease.business.Servlet2</servlet-class>
    <load-on-startup>1</load-on-startup>  
  </servlet>    
 
  <servlet>  
   <servlet-name>servlet3</servlet-name>  
    <servlet-class> com.javawithease.business.Servlet3</servlet-class>
    <load-on-startup>-1</load-on-startup>  
  </servlet>  
 
  //other attributes
 
</web-app>
In the above example Servlet1 and Servlet2 will be loaded when server starts because positive value is passed in there load-on-startup. Servlet3 will be loaded at the time of first request because negative value is passed in there load-on-startup.

Welcome File List: Previous                                                      Next: Request Dispatcher Interface