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

时间:2011-10-18 06:38:08

标签: java java-ee servlets

我是J2EE的新手,请考虑同样的答案。当我们使用struts时为什么要在servlet标记中写&lt; load-on-startup>2</load-on-startup>?这个标签是什么意思?如果有什么东西加载秒,那么先加载什么另请提供一些链接,它解释了structs-config.xml

的所有标签

5 个答案:

答案 0 :(得分:10)

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>

仔细阅读文档。

答案 1 :(得分:8)

请参阅http://struts.apache.org/1.x/userGuide/configuration.html

load-on-startup表示必须在启动webapp时加载和初始化servlet(即,一旦部署,就不必等待对servlet的请求)。该数字表示初始化的顺序。如果另一个servlet有1,它将被加载。如果另一个有3,则会在之后加载。

答案 2 :(得分: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之前加载。

答案 3 :(得分:2)

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

答案 4 :(得分:0)

1)是“web.xml”中使用的元素。

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

3)订单基于标签内提供的数字    示例:1             2   1 -server首先执行,然后移动到2 ..,