July 27, 2012

What does the 'load-on-startup' element mean in Deployment Descriptor (web.xml)?

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.

This element of the Deployment Descriptor (web.xml) is used to determine
the order of initializing the particular servlet when the application
server starts up. The content of this element is expected to be a positive
integer and lower the number, earlier the particular servlet will be
initialized during server start up.

If the content is not a positive integer (or if no value is specified as
the content) then the application server is free to initialize the servlet
any time it wants during the server start up.

This maybe an application server dependent thing... but, I guess it's the
same for almost all popular application servers currently being used like
WebLogic, WebSphere, Oracle Application Server 10g, etc.

load-on-startup for Servlets:

......
<servlet id="servlet1">
<servlet-name>NameOfTheServlet</servlet-name>
<display-name>DisplayNameOfTheServlet</display-name>
<servlet-class>FullyQualifiedServletClassName</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
......

load-on-startup for JSPs:

......
<servlet>
<servlet-name>NameOfTheServletGenerated</servlet-name>
<jsp-file>RelativePathOfTheJSPFile</jsp-file>
<load-on-startup>3</load-on-startup>
</servlet>
......

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.[...]"

We can specify the order in which we want to initialize various Servlets.
Like first initialize Servlet1 then Servlet2 and so on.
This is accomplished by specifying a numeric value for the
<load-on-startup> tag. <load-on-startup> tag specifies that the servlet
should be loaded automatically when the web application is started.

The value is a single positive integer, which specifies the loading order.
Servlets with lower values are loaded before servlets with
higher values (ie: a servlet with a load-on-startup value of 1 or 5 is
loaded before a servlet with a value of 10 or 20).

When loaded, the init() method of the servlet is called. Therefore this tag
provides a good way to do the following:

start any daemon threads, such as a server listening on a TCP/IP port, or a
background maintenance thread perform initialisation of the application,
such as parsing a settings file which provides data to other servlets/JSPs
If no <load-on-startup> value is specified, the servlet will be loaded when
the container decides it needs to be loaded - typically on it's first
access. This is suitable for servlets that don't need to perform special
initialisation.



As per my understanding the value for the <load-on-startup> has to be a
positive integer in order for it to get loaded automatically.

7 comments:

  1. bookmarked!!, I love your site!

    Also visit my web page :: CrossFit clothing for women

    ReplyDelete
  2. I enjoy what you guys tend to be up too. This type of clever work
    and exposure! Keep up the amazing works guys I've added you guys to
    my personal blogroll.

    Feel free to surf to my page annuity lead generation

    ReplyDelete
  3. I love it when folks get
    together and share ideas. Great site, stick with
    it!

    Here is my weblog ... kindle fire

    ReplyDelete
  4. Greetings from Idaho!

    I'm bored to death at work so I decided to browse your website on my iphone during lunch break. I really like the info you present here and can't wait to
    take a look when I get home. I'm amazed at how quick your blog loaded on my mobile .. I'm not even
    using WIFI, just 3G .. Anyhow, amazing site!


    my webpage - Sytropin Reviews

    ReplyDelete
  5. Howdy! This post could not be written any better!
    Looking at this article reminds me of my previous roommate!
    He constantly kept talking about this. I'll send this post to him. Pretty sure he's going
    to have a very good read. Thanks for sharing!


    Feel free to visit my page ... free weather alerts

    ReplyDelete
  6. I love it when people come together and share thoughts.
    Great site, continue the good work!

    Feel free to visit my website; Plenty of Fish login (http://yourpoflogin.com/)

    ReplyDelete
  7. I couldn't resist commenting. Perfectly written!

    Also visit my homepage :: public safety alerts

    ReplyDelete

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts