10

I am very new to J2EE considering the same please answer. When we use struts why we write <load-on-startup>2</load-on-startup> in the servlet tag? What does this tag means? If something load seconds then what loads first? Also please provide some links which explains me all the tags of structs-config.xml

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
amod
  • 4,190
  • 10
  • 49
  • 75

5 Answers5

11

load-on-startup tells the servlet container to load the specified resource at server startup. The number that you see tells the order of startup if there are more than one load-on-startup tag.

<load-on-startup>1</load-on-startup>
<load-on-startup>2</load-on-startup>

will cause the resource with load on startup 1 to be loaded first. This is to control the sequence of loading if there is a dependency. Look at the servlet specification that explains the load sequence.

The answer I referred to in my comment below (Ref http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd):

  <xsd:element name="load-on-startup"
           type="javaee:load-on-startupType"
           minOccurs="0">
    <xsd:annotation>
      <xsd:documentation>

        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.

      </xsd:documentation>
    </xsd:annotation>
  </xsd:element>

Read the documentation carefully.

Ayusman
  • 8,509
  • 21
  • 79
  • 132
  • if i write 1 will it create any difference??? Can provide me some links??? – amod Oct 19 '11 at 07:15
  • @amod0017 since you are looking for details... hit this link in your browser "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" and search for the element "" and read the documentation. There is no better place than the servlet specifications (docs and XSDs) and API :-) hope this helps. – Ayusman Oct 21 '11 at 06:06
  • 1
    Adding the content for everybody's benefit in my answer... – Ayusman Oct 21 '11 at 06:09
9

See http://struts.apache.org/1.x/userGuide/configuration.html.

load-on-startup means that the servlet must be loaded and initialized on startup of the webapp (i.e. as soon as it is deployed, without waiting for a request to the servlet). The number indicates the order of the initialisations. If another servlet has 1, it will be loaded before. If another has 3, it will be loaded after.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • so if i write 1 in web.xml it will not create any difference??? I guess it will isnt it? In web i always find 1 is written. – amod Oct 18 '11 at 09:45
  • 1
    If the servlet is the only one in the web.xml, it won't create any difference. If some other servlet has 1, then the order is indeterminate, AFAIK. – JB Nizet Oct 18 '11 at 10:01
3

If you are using Tomcat, there are a couple of servlets that are loaded for every webapplication:

  • the default servlet (usually serves static content and respond to any unmapped url)
  • the jsp servlet

Taking a look at the default Tomcat's web.xml configuration file...

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    ...
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    ...
    <load-on-startup>3</load-on-startup>
</servlet>

Notice that default is at 1 while jsp is at 3.

So, using <load-on-startup>2</load-on-startup> means that your servlet will be loaded at deploy time, after the default servlet but before the jsp servlet.

namero999
  • 2,882
  • 18
  • 24
  • This makes sense @namero999 - will the same apply if using spring? I mean should I still give the value 2 for load on startup for my servlet in web.xml? – Nirmal Jan 24 '17 at 16:49
  • I guess yes. I mean, the concept applies in general, regardless of Spring, therefore you should be good. – namero999 Jan 25 '17 at 15:10
  • I am trying to find out the definitive on this. It has to be different for spring on that front because spring's context load listener starts early and we don't specify anything for that. – Nirmal Jan 25 '17 at 15:13
2

load-on-startup tells the container to load the servlet during application startup. The number assigned is the rank of the servlet which tells the order in which the load-on-servlet's should be loaded.

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
0

1) is an element which is used in "web.xml".

2)This element indicates the web-container to Load the server indicated by this element.

3)The order is based on the number provided inside the tag Example: 1 2 1 -server executed first,then it moves to 2..,

  • There's no point of repeating an already given answer. This is a Q&A site, not an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess upon agreement. – BalusC Aug 28 '15 at 11:57