• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

<load-on-startup> order for entries with the same value

 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Ranchers!

I'm reading K&B HF book - chapter 11 about servlet initialization (pp. 628 - "Configuring servlet initialization in the DD"). I bumped into the <load-on-startup> section which talks that:

And what if there's more than one servlet with a <load-on-startup> of 4? The Container loads servlets with the same value in the order in which the servlets are declared in the DD.


And the specs for Servlets 2.4 and 3.0 says in sections 13.4 (10 - servlet Element) and 14.4 (10 - servlet Element) respectively that:

The element load-on-startup indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the Web application. The element content of this element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-startup value.


So, in fact, the order isn't defined by the DD. I've tested it and it doesn't follow the DD order, so - watch out for this guys :-)

Cheers!

PS. A curiosity. The specs says that it should be an integer number you pass into the <load-on-startup>, but in fact Tomcat 6 does allow you to pass a float-point number. It does however translates the value to the integer before working on it. So:



Tomcat 7 will throw NumberFormatException if you try to do this :-)
 
Prashant Shiralkar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro Kowalski wrote: I've tested it and it doesn't follow the DD order, so - watch out for this guys :-)

I can't think of a situation where you'd keep same values for <load-on-startup> yet bother about the order. How does it matter?
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Prashant, short answer - think about the exam.
Just like in OCPJP - there are some questions which are hard to believe to be used in real-life, but still you need to know how the compiler/container will work in order to pass. Furthermore knowledge that some construction is weird, isn't the same as knowledge about what happens when it occurs.

Treat it also as a warning message, that the info in Head First book is quite inaccurate.

Cheers!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it related to page 596 of the errata ?
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, in fact it is :-)

PS. What is this errata site you posted?
I've looked at this: http://oreilly.com/catalog/errata.csp?isbn=9780596516680

and there isn't the one which is on site pointed by you?

Cheers!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mine is for the first edition.
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I guess they omit this one, as it is still printed the same in the second edition.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dooh
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Pedro Kowalski , for putting this information in nice and clear way. its good to know the exact behavior but I guess implementation might vary from container to container , you have checked in Tomcat might be worth checking in weblogic or websphere to see how they have implemented this.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic