gpt4 book ai didi

java - Apache Tomcat 执行具有相同启动时加载的 Servlet

转载 作者:行者123 更新时间:2023-11-28 22:06:12 28 4
gpt4 key购买 nike

我最近一直在尝试 Java Servlet 技术的几个方面。最近,我遇到了 web.xml 的 load-on-startup 标签,它属于 Servlet 规范。我试验了启动时加载在 Apache Tomcat 中的工作方式,并通过日志意识到每个 servlet 的执行 servlet init() 方法的顺序很可能是基于 servlet 名称的字母顺序而不是 servlet 的顺序在 web.xml 中定义。

web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<display-name>Hello-World-Servlet</display-name>

<servlet>
<servlet-name>hello-world</servlet-name>
<servlet-class>org.wso2.carbon.HelloWorld</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>all-is-well</servlet-name>
<servlet-class>org.wso2.carbon.HelloWorld</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>bonjour-monde</servlet-name>
<servlet-class>org.wso2.carbon.HelloWorld</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>hello-world</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>all-is-well</servlet-name>
<url-pattern>/well</url-pattern>
<url-pattern>/alright</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>bonjour-monde</servlet-name>
<url-pattern>/bonjour</url-pattern>
</servlet-mapping>

</web-app>

servlet类如下:

public class HelloWorld extends HttpServlet {
private static final Logger logger = Logger.getLogger(HelloWorld.class.getName());

@Override
public void init() throws ServletException {
logger.log(Level.INFO, "The servlet " + getServletName() + " is starting...");
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println(getServletName());
if (getServletName().equals("hello-world")) {
response.getWriter().println("Hello World!!!");
} else if (getServletName().equals("bonjour-monde")) {
response.getWriter().println("Bonjour monde!!!");
} else if (getServletName().equals("all-is-well")) {
response.getWriter().println("All izz well!!!");
}
}

@Override
public void destroy() {
logger.log(Level.INFO, "The servlet " + getServletName() + " is getting destroyed...");
}
}

当我检查 Tomcat 日志时,它如下所示:

15-Apr-2016 14:41:12.678 INFO [localhost-startStop-1] org.wso2.carbon.HelloWorld.init The servlet all-is-well is starting...
15-Apr-2016 14:41:12.680 INFO [localhost-startStop-1] org.wso2.carbon.HelloWorld.init The servlet bonjour-monde is starting...
15-Apr-2016 14:41:12.680 INFO [localhost-startStop-1] org.wso2.carbon.HelloWorld.init The servlet hello-world is starting...

我遇到了几个与此行为相矛盾的资源,并提到如果启动时加载对于多个 servlet 是相等的,则它们在描述符中出现的顺序决定顺序。但是当引用下面的What does the servlet <load-on-startup> value signify ,该规范似乎有不同的想法,指出它取决于容器的实现。这与我的经历相吻合

为这种情况定义的最准确的机制是什么?

最佳答案

阅读JSR 340 :

The element load-on-startup indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the Web application. The element content of this 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-startup value.

[强调我的]

关于java - Apache Tomcat 执行具有相同启动时加载的 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36643327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com