【问题标题】:why we write <load-on-startup>2</load-on-startup> in web.xml while using struts 1.x? [duplicate]为什么我们在使用 struts 1.x 时在 web.xml 中编写 <load-on-startup>2</load-on-startup> ? [复制]
【发布时间】:2011-12-09 20:10:37
【问题描述】:

考虑到同样的问题,我对 J2EE 很陌生,请回答。当我们使用 struts 时,为什么要在 servlet 标签中写 load-on-startup>2</load-on-startup>?这个标签是什么意思?如果加载几秒钟,那么首先加载什么?还请提供一些链接来解释 structs-config.xml 的所有标签

【问题讨论】:

    标签: java jakarta-ee servlets


    【解决方案1】:

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

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

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

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

    仔细阅读文档。

    【讨论】:

    • 如果我写 1 会有什么不同吗???可以给我一些链接吗???
    • @amod0017 因为您正在寻找详细信息...在浏览器中点击此链接“java.sun.com/xml/ns/javaee/web-app_2_5.xsd”并搜索元素“" 并阅读文档。没有比 servlet 规范(文档和 XSD)和 API 更好的地方了 :-) 希望这会有所帮助。
    • 在我的回答中为大家的利益添加内容...
    【解决方案2】:

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

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

    【讨论】:

    • 所以如果我在 web.xml 中写 1 它不会产生任何区别???我想不会吧?在网络上我总是发现 1 是写的。
    • 如果 servlet 是 web.xml 中唯一的一个,它不会产生任何差异。如果其他一些 servlet 有 1,那么顺序是不确定的,AFAIK。
    【解决方案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。

    因此,使用&lt;load-on-startup&gt;2&lt;/load-on-startup&gt; 意味着您的 servlet 将在部署时加载,在默认 servlet 之后但在 jsp servlet 之前。

    【讨论】:

    • 这是有道理的@namero999 - 如果使用弹簧,同样适用吗?我的意思是我是否应该在 web.xml 中为我的 servlet 提供启动时加载的值 2?
    • 我猜是的。我的意思是,这个概念普遍适用,不管 Spring,所以你应该是好的。
    • 我正试图找出这方面的权威。在这方面,spring 必须有所不同,因为 spring 的上下文负载侦听器很早就开始了,我们没有为此指定任何内容。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

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

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

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

      【讨论】:

      • 重复已经给出的答案是没有意义的。这是一个问答网站,而不是一个老式的讨论论坛,每个人都在同意后互相重复,变得难以消化。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      相关资源
      最近更新 更多