views:

13286

answers:

6

Hi,

I am getting a bit confused here. In our application we are having a few servlets defined. Here is the excerpt from the web.xml for one of the servlets:

    <servlet>
     <servlet-name>AxisServlet</servlet-name>
     <display-name>Apache-Axis Servlet</display-name>
     <servlet-class>com.cisco.framework.axis2.http.FrameworkServlet</servlet-class>
     <load-on-startup>0</load-on-startup>
    </servlet> 

As per my understanding the value for the has to be a positive integer in order for it to get loaded automatically. I looked up on google but the responses I came across only added to my confusion.

-  Ashish

+4  A: 

It indicates that the servlet won't be started until a request tries to access it.

If load-on-startup is greater than zero then when the container starts it will start that servlet in ascending order of the load on startup value you put there (ie 1 then 2 then 5 then 10 and so on).

cletus
Zero causes loading, too. Only negative values are not garanteed:"[...]If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed.[...]"
marabol
+4  A: 

While I believe that cletus is correct in his answer, Resin seems to document something different:

load-on-startup can specify an (optional) integer value. 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.

Thus, you probably want to check not only the spec, but also the documentation for your web container.

Eddie
A: 

Servlet Life Cycle

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.

  1. If an instance of the servlet does not exist, the Web container

    1.1. Loads the servlet class.

    1.2. Creates an instance of the servlet class.

    1.3. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.

  2. Invokes the service method, passing a request and response object. Service methods are discussed in the section Writing Service Methods.

A 0 value on load-on-startup means that point 1 is executed when a request come to that servlet. Other values means that point 1 is executed at container startup.

SourceRebels
+2  A: 

Short: value >= 0 means that the servlet is loaded when the web-app is deployed or when the server starts. value < 0 : servlet is loaded whenever the container feels like.

Long answer(from the spec): "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 128 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."

A: 

Note you should always set load-on-startup element to be 0 so the servlet is initialized when the server is started. This will prevent any delay for the first client which uses the application.

Srikanth
A: 

I have question then, what will happen if the 2 servlets are having the same integer value setted as 1 in the load-on-startup ?

Jitendra
BalusC