load-on-startup:
The load-on-startup is the sub-attribute of the servlet attribute in web.xml. It is used to control when the web server loads the servlet. As we discussed the servlet is loaded at the time of the first request. In this case, response time is increased for the 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 the first request.
Any positive or negative value can be passed in the load-on-startup. If a positive value is passed then all servlets having load-on-startup sub-attribute will be loaded when the server starts but a servlet having a low positive value will be loaded first. In case of a negative value servlet will be loaded at the time of the first request.
Sample code of load-on-startup in web.xml.
<web-app> //other attributes <servlet> <servlet-name>servlet1</servlet-name> <servlet-class>com.w3schools.business.Servlet1 </servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet> <servlet-name>servlet2</servlet-name> <servlet-class> com.w3schools.business.Servlet2</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>servlet3</servlet-name> <servlet-class> com.w3schools.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 the server starts because a positive value is passed in their load-on-startup. Servlet3 will be loaded at the time of the first request because a negative value is passed in their load-on-startup.