Load On Startup
<load-on-startup> Element in Servlet
This is a web.xml configuration elements used to configure a servlet to create servlet object during startup time of the server or application deploy on server. It is also known as pre initialization of servlet. This element need integer value to configure.
Advantage of load-on-startup element
As we know well, servlet is loaded at first request. That means it consumes more time at first request. If we 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 we 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
<web-app> .... <servlet> <servlet-name>servlet1</servlet-name> <servlet-class>com.tutorial4us.FirstServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet> <servlet-name>servlet2</servlet-name> <servlet-class>com.tutorial4us.SecondServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> ... </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 a negative value is configure then a container ignores this tag and waits for first request to create an object of a servlet but a container does not throws any exception.