aspose file tools*
The moose likes Servlets and the fly likes load-on-startup with 0 value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "load-on-startup with 0 value" Watch "load-on-startup with 0 value" New topic
Author

load-on-startup with 0 value

Abhishek Rath
Ranch Hand

Joined: Nov 22, 2007
Posts: 38
Hi

I have a servlet and the entry in the web.xml is as follows

<servlet>
<servlet-name>WebGraph</servlet-name>
<display-name>WebGraph</display-name>
<servlet-class>a.b.c.Graph</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>

Also there is no init method specified in the above servlet.

Please let me know if the same servlet will be initialised during server start up?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

If the value is 0 or greater, it indicates an order for servlets to be loaded, servlets with higher numbers get loaded after servlets with lower numbers.

some containers like tomcat don't process entries with a value less than 0.
Vamsi Mohan
Ranch Hand

Joined: Jun 20, 2009
Posts: 80
If you give the value as "0" as load-on-startup, the servlet automatically started/initiated while startup without any user call. This is called "Dynamic Loading". If you give "1", the user has to call the servlet explicitly. This is called "Lazy Loading".


VAMSI MOHAN.V
(SCJP5.0, IBM Rational Solution Designer, IBM Rational Architect, IBM RUP Designer)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Vamsi Mohan wrote:If you give the value as "0" as load-on-startup, the servlet automatically started/initiated while startup without any user call. This is called "Dynamic Loading". If you give "1", the user has to call the servlet explicitly. This is called "Lazy Loading".

Completely incorrect. Any non-negative value for load-on-startup will cause the servlet to load on startup. A negative value leaves it up to the container.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 254

Hey Abhishek,

I am pasting few line from hfsj page# 628, I believe, this would clear all your doubts. Regarding load-on-startup option.

If you want servlets to be loaded at deploy time (or at server restart time) rather than on first request, use the <load-on-startup> element in the DD. Any non-negative value for <load-on-startup> tells the Container to initialize the servlet when the app is deployed (or any time the server restarts).

If you have multiple servlets that you want preloaded, and you want to control the order in which they’re initialized, the value of <load-on-startup> determines the order! In other words, any non-negative value means load early, but the order in which servlets are loaded is based on the value of the different <load-on-startup> elements.

<load-on-startup>4</load-on-startup> does NOT mean “load four instances of the servlet”. It means that this servlet should be loaded only AFTER servlets with a <load-on-startup> number less than four are loaded. 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.


Regards
Salil Verma
Praveen Kumar Gupta
Greenhorn

Joined: Oct 13, 2009
Posts: 1


Hi All
<load-on-startup>-1</load-on-startup>
negative value shows that servlet object will be instantiate when the first request comes.

<load-on-startup>x</load-on-startup>
x = 0,1,2,3 and so on...

define that servlet object will be instantiate when the server starts.

and the x value difine the priority of the servet which instantiate first respectively



Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 254

x value difine the priority of the servet which instantiate first respectively


it should be that +ive x value define the reverse order of priority. That means higher the value= lower the priority.
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Abhishek Rath wrote:Hi

I have a servlet and the entry in the web.xml is as follows

<servlet>
<servlet-name>WebGraph</servlet-name>
<display-name>WebGraph</display-name>
<servlet-class>a.b.c.Graph</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>

Also there is no init method specified in the above servlet.

Please let me know if the same servlet will be initialised during server start up?


Abhishek,

To answer your question yes the servlet will be loaded and it doesn't matter whether the init method is implemented or not and if it is implemented it is guaranteed to be called before servlet handles any request.

hope this helps.

Thanks,
Rahul


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Abhishek Rath
Ranch Hand

Joined: Nov 22, 2007
Posts: 38
Thanks for the details.
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
Bear Bibeault wrote:
Vamsi Mohan wrote:If you give the value as "0" as load-on-startup, the servlet automatically started/initiated while startup without any user call. This is called "Dynamic Loading". If you give "1", the user has to call the servlet explicitly. This is called "Lazy Loading".

Completely incorrect. Any non-negative value for load-on-startup will cause the servlet to load on startup. A negative value leaves it up to the container.


it depends on container also.


:d
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10065
    
163

pooja jain wrote:
Bear Bibeault wrote:
Vamsi Mohan wrote:If you give the value as "0" as load-on-startup, the servlet automatically started/initiated while startup without any user call. This is called "Dynamic Loading". If you give "1", the user has to call the servlet explicitly. This is called "Lazy Loading".

Completely incorrect. Any non-negative value for load-on-startup will cause the servlet to load on startup. A negative value leaves it up to the container.


it depends on container also.


As Bear said, for negative numbers it's up to the container to decide. But it does *not* depend on containers to decide, what load order they follow for positive numbers. The order is mandated by the spec (which by the way is really very clear about the load-on-startup):

The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these 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-start-up value.


[My Blog] [JavaRanch Journal]
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
thanks jaikiran & bear.
goyankavicky gupta
Greenhorn

Joined: Sep 07, 2010
Posts: 1
thanks n & bear.
G krishna kumar
Greenhorn

Joined: Oct 10, 2012
Posts: 14
for load-on-startup tag in web.xml it is always recomanded to have a positive number or zero.
load-on-startup : it defines when the object has to created based on the priority of a given number.
here zero has the highest priority.
if two servlet are having the same priority then it deffer from server to server when the object will be created for the respective servlet.
for example :
in tomcat... it chooses based on the alphabetical order.
in case of web logic the server which is configured first will be given the first priority.
in case of negative number the server will discard the the tag load on startup.
 
Don't get me started about those stupid light bulbs.
 
subject: load-on-startup with 0 value