Issue
When we mention any servlet as loadOnStartup in web.xml then its init method is called at applicaton startup.
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd version="3.0">
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>app01c.SimpleServlet</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/simple</url-pattern>
</servlet-mapping>
</web-app>
<load-on-startup>10</load-on-startup>
here, what does it mean for the value 10? if i change it to 5, what will happen? having less value will make it to load earlier? if so, if it is 0, is it the earliest? i am little confused with as i came across some googling that positive value in load-on-startup, make it to load at start up. Does this positive is greater than than 0? Does 0 value is same as nothing in load-on-startup?
Solution
0 is the highest priority.
If you have only one servlet you can not see the difference
ServletName
load-on-start-up_value
Servlet1 4(3)
Servlet2 6(4)
Servlet3 3(2)
Servlet4 2(1)
Servlet4 object will be created first then Servlet3 object will be created and then Servlet1 and Servlet2 objects will be created.
If you give -1
it will be ignored
Answered By - PSR
Answer Checked By - Cary Denson (JavaFixing Admin)