Search code examples
servletsmappingpattern-matchingmultiple-matches

Servlet mapping with multiple matches


I have a question according the mapping behaviour.

If there are these mappings given:

<servlet-mapping>
   <servlet-name> ServletA </servlet-name>
   <url-pattern> *.xml </url-pattern>
</servlet-mapping>
<servlet-mapping>
   <servlet-name> ServletB </servlet-name>
   <url-pattern> /result/* </url-pattern>
</servlet-mapping>

And there is this HTTP-Request:

/result/example.xml

Which servlet would be mapped and why?


Solution

  • Given your <servlet-mapping> configuration, the request for /result/example.xml would be handled by ServletB because a path match always trumps an extension match.

    This is so because an extension match is considered to be a looser (not loser, though that works too :) constraint as it works site-wide when compared to a path match which targets a specific directory and its descendents and is hence more specific in nature.

    The matching priority goes like this:

    1. Exact path match
    2. Wildcard path match (a longer match will trump a shorter one)
    3. Extension match
    4. Default servlet (configured with just / to handle all 404s)