0

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?

PSR
  • 39,804
  • 41
  • 111
  • 151
CrawlingKid
  • 959
  • 5
  • 15
  • 1
    [possible duplicate](http://stackoverflow.com/questions/809775/what-does-the-servlet-load-on-startup-value-of-0-zero-signify) ... btw first goolge search result for `web.xml ` – A4L May 15 '13 at 19:06
  • 1
    In the [Java Servlet 3.0 Specification](http://download.oracle.com/otndocs/jcp/servlet-3.0-mrel-eval-oth-JSpec/), read in the **14.4 Deployment Descriptor Diagram** chapter, item "**10 servlet Element**" on page 172 about `load-on-startup`. – informatik01 May 15 '13 at 19:26

2 Answers2

3

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

PSR
  • 39,804
  • 41
  • 111
  • 151
1

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.

SkyWalker
  • 28,384
  • 14
  • 74
  • 132