You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mladen Adamović <ml...@gmail.com> on 2016/10/12 07:02:41 UTC

web.xml and @WebServlet priority when more rules match

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 12.2 states: The container will recursively try to match the longest
path-prefix. This is done by stepping down the path tree a directory at a
time, using the ’/’ character as a path separator. The longest match
determines the servlet selected.

So for the request /something/error Tomcat will choose Two.class?

And more general, in the case there are both annotation and web.xml rule
with the same prefix length, it will choose a first occurrence in web.xml?

If no one knows the answers but knows where to look in the source code,
that could be beneficial as well.

Thanks

Re: web.xml and @WebServlet priority when more rules match

Posted by Mark Thomas <ma...@apache.org>.
On 12/10/2016 08:02, Mladen Adamovi\u0107 wrote:
> 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.`

Note that the order of precedence is:
web.xml -> web-fragment.xml -> annotations

> While 12.2 states: The container will recursively try to match the longest
> path-prefix. This is done by stepping down the path tree a directory at a
> time, using the \u2019/\u2019 character as a path separator. The longest match
> determines the servlet selected.
> 
> So for the request /something/error Tomcat will choose Two.class?

No. Neither will match. Prefix matches have to end with "/*". The two
mappings above are exact matches.

> And more general, in the case there are both annotation and web.xml rule
> with the same prefix length, it will choose a first occurrence in web.xml?

The mapping from web.xml will be used.

If there are two mappings in web.xml with identical URL patterns then an
error will occur.

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org