简体   繁体   English

两个规则匹配时的web.xml和@WebServlet优先级(根据Servlet 3.0规范)

[英]web.xml and @WebServlet priority when two rules match (according to Servlet 3.0 spec)

Lets assume that web.xml has a rule 假设web.xml具有规则

<servlet-mapping>
        <servlet-name>One</servlet-name>
        <url-pattern>/something/e*</url-pattern>
</servlet-mapping>

While class Two.class has an annotation: 虽然Two.class类具有注释:

@WebServlet(name = "Two", urlPatterns = {"/something/er*"})

From the Servlet 3.0 specification 来自Servlet 3.0规范

8.2.3. 8.2.3。 (point 4). (第4点)。 The web.xml of the web application has the highest precedence when resolving conflicts between the web.xml, web-fragment.xml and annotations.

While the same specification 12.1 says The Web application selected must have the longest context path that matches the start of the request URL 尽管相同的规范12.1说The Web application selected must have the longest context path that matches the start of the request URL

So if I understand this correctly, 12.1 rule means that the request /something/error will invoke servlet Two? 因此,如果我正确理解这一点,则12.1规则意味着请求/something/error将调用servlet Two? Rule 12.1 will have the priority over the rule 8.2.3 point 4 (no conflict detected between web.xml and annotation)? 规则12.1优先于规则8.2.3第4点(在web.xml和注释之间未检测到冲突)?

Am I getting this right? 我说对了吗? Or is this a conflict according to 8.2.3 point 4? 还是根据8.2.3第4点,这是冲突?

The Spec at 12.1 is talking about the context path of the web module. 12.1的规范讨论的是Web模块的上下文路径。 This is used by the servlet container to basically know what .war to use. servlet容器使用它来基本知道要使用什么.war。 In this case "something" is not the context path (or shouldn't be). 在这种情况下,“某物”不是上下文路径(也不应该是)。

On 12.1 too: 同样在12.1上:

The path used for mapping to a servlet is the request URL from the request object minus the context path and the path parameters. 用于映射到servlet的路径是来自请求对象的请求URL减去上下文路径和路径参数。

Therefore, your quote on 12.1 does not affect 8.2.3 and the precedence is as you have quoted: 因此,您对12.1的引用不会影响8.2.3,并且优先级如您所引用:

The web.xml of the web application has the highest precedence when resolving conflicts between the web.xml, web-fragment.xml and annotations. 解决web.xml,web-fragment.xml和注释之间的冲突时,Web应用程序的web.xml具有最高优先级。

In your example, servlet One will be called if the request goes to host:port/contextPath/something/error 在您的示例中,如果请求发送到host:port / contextPath / something / error,则将调用Servlet One。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM