为什么在使用struts 1.x时我们需要在web.xml中写<load-on-startup>2</load-on-startup>?

10
我很不熟悉J2EE,请问当我们使用Struts时,为什么会在servlet标签中写上<load-on-startup>2</load-on-startup>?这个标签是什么意思?如果有些东西需要几秒钟才能加载,那么哪个会先加载?另外,可否提供一些解释structs-config.xml所有标签的链接?
5个回答

11

load-on-startup告诉Servlet容器在服务器启动时加载指定的资源。您看到的数字表示启动顺序,如果有多个load-on-startup标记。

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

如果存在依赖关系,将导致具有启动加载1的资源先被加载。 这是为了控制加载顺序。 可以查看解释加载顺序的servlet规范。

我在下面的评论中提到的答案(参考 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>

仔细阅读文档。


如果我写了<load-on-startup>1</load-on-startup>会有什么区别吗?可以给我提供一些链接吗? - amod
@amod0017,既然你正在寻找细节...请在浏览器中点击此链接“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">”,并阅读文档。没有比Servlet规范(文档和XSD)和API更好的地方了 :-) 希望这可以帮到你。 - Ayusman
1
在我的回答中添加内容,以使每个人受益... - Ayusman

9
请查看http://struts.apache.org/1.x/userGuide/configuration.htmlload-on-startup表示servlet必须在webapp启动时加载和初始化(即在部署后立即加载,而不必等待对servlet的请求)。数字表示初始化的顺序。如果另一个servlet具有1,则它将在其之前加载。如果另一个具有3,则它将在其之后加载。

如果我在web.xml中写入<load-on-startup>1</load-on-startup>,这不会产生任何区别吗?我猜它会产生区别,不是吗?在web中,我总是发现写有<load-on-startup>1</load-on-startup>。 - amod
1
如果servlet是web.xml中唯一的一个,那么它不会产生任何区别。如果其他servlet有1个,那么顺序是不确定的,据我所知。 - JB Nizet

3

如果您正在使用Tomcat,每个Web应用程序都会加载一些servlet:

  • 默认servlet(通常用于提供静态内容并响应任何未映射的URL)
  • JSP servlet

查看默认Tomcat的web.xml配置文件...

<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>

请注意,默认值为1,而jsp的值为3。

因此,使用<load-on-startup>2</load-on-startup>表示您的servlet将在部署时加载,在默认servlet之后但在jsp servlet之前加载。


这很有道理,@namero999 - 如果使用Spring,是否也适用相同的方法?我的意思是,在web.xml中为我的servlet仍然应该给出值2来进行启动加载吗? - Nirmal
我想是的。我的意思是,这个概念通常适用,不论是否使用Spring,所以你应该没问题。 - namero999
我正在努力找到这方面的明确答案。因为Spring的上下文加载监听器会提早启动且我们没有针对它指定任何内容,所以在这方面必须有所不同。 - Nirmal

2

load-on-startup指示容器在应用程序启动期间加载servlet。分配的数字是servlet的等级,它告诉load-on-servlet应该以何种顺序加载。


0

1) "<load-on-startup>"是在"web.xml"中使用的元素。

2)该元素指示Web容器加载此元素指定的服务器。

3)顺序基于标签内提供的数字。例如:
<load-on-startup>1</load-on-startup>
<load-on-startup>2</load-on-startup>
1-先执行服务器,然后移动到2..


没有必要重复已经给出的答案。这是一个问答网站,而不是一个老式的讨论论坛,在那里每个人在达成一致意见后都会互相重复,导致难以理解的混乱。 - BalusC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接