【发布时间】:2011-12-09 20:10:37
【问题描述】:
考虑到同样的问题,我对 J2EE 很陌生,请回答。当我们使用 struts 时,为什么要在 servlet 标签中写 load-on-startup>2</load-on-startup>?这个标签是什么意思?如果加载几秒钟,那么首先加载什么?还请提供一些链接来解释 structs-config.xml 的所有标签
【问题讨论】:
标签: java jakarta-ee servlets
考虑到同样的问题,我对 J2EE 很陌生,请回答。当我们使用 struts 时,为什么要在 servlet 标签中写 load-on-startup>2</load-on-startup>?这个标签是什么意思?如果加载几秒钟,那么首先加载什么?还请提供一些链接来解释 structs-config.xml 的所有标签
【问题讨论】:
标签: java jakarta-ee servlets
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>
仔细阅读文档。
【讨论】:
见http://struts.apache.org/1.x/userGuide/configuration.html。
load-on-startup 表示 servlet 必须在 webapp 启动时加载和初始化(即,一旦部署,无需等待对 servlet 的请求)。数字表示初始化的顺序。如果另一个 servlet 有 1,它将在之前加载。如果另一个有 3,它将在之后加载。
【讨论】:
如果您使用的是 Tomcat,则会为每个 Web 应用程序加载几个 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 之前。
【讨论】:
load-on-startup 告诉容器在应用程序启动期间加载 servlet。 分配的数字是 servlet 的等级,它告诉 load-on-servlet 的加载顺序。
【讨论】:
1) 是“web.xml”中使用的元素。
2)此元素指示要加载此元素指示的服务器的网络容器。
3) 订单以标签内提供的数字为准 示例:1 2 1 -server 先执行,然后移动到 2..,
【讨论】: