Mladen Adamovic
Mladen Adamovic

Reputation: 3211

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

Lets assume that web.xml has a rule

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

While class Two.class has an annotation:

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

From the Servlet 3.0 specification

8.2.3. (point 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

So if I understand this correctly, 12.1 rule means that the request /something/error will invoke 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)?

Am I getting this right? Or is this a conflict according to 8.2.3 point 4?

Upvotes: 0

Views: 1945

Answers (1)

Harold
Harold

Reputation: 76

The Spec at 12.1 is talking about the context path of the web module. This is used by the servlet container to basically know what .war to use. In this case "something" is not the context path (or shouldn't be).

On 12.1 too:

The path used for mapping to a servlet is the request URL from the request object minus the context path and the path parameters.

Therefore, your quote on 12.1 does not affect 8.2.3 and the precedence is as you have quoted:

The web.xml of the web application has the highest precedence when resolving conflicts between the web.xml, web-fragment.xml and annotations.

In your example, servlet One will be called if the request goes to host:port/contextPath/something/error

Upvotes: 1

Related Questions