Reputation: 994
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?
Upvotes: 0
Views: 875
Reputation: 399
By default, servlet object is created when you make the first request to servlet, but if you want to create the servlet object at the load time(or start up time), then you can provide <load-on-startup></load-on-startup>
value in web.xml.
<load-on-startup></load-on-startup>
is servlet wise. If there are 2 servlets in your application so you need to provide <load-on-startup></load-on-startup>
value for each servlet.
<load-on-startup></load-on-startup>
value either 0 or any positive integer. If you are putting <load-on-startup>10</load-on-startup>
value 10, and you have only single servlet then it will not affect any thing, but you have more then 1 servlet then 0 value is highest priority and then so on.
Upvotes: 2
Reputation: 40318
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
Upvotes: 3