20

I'm wondering what is the difference of using a single or double wildcards to describe a url-pattern on a servlet mapping.

For example: what is the difference below?

1)

<servlet-mapping id="...">
    <servlet-name>BuyServlet</servlet-name>
    <url-pattern>/buy/*</url-pattern>
</servlet-mapping>

2)

<servlet-mapping id="...">
    <servlet-name>ShopServlet</servlet-name>
    <url-pattern>/shop/**</url-pattern>
</servlet-mapping>

EDIT: @Andrew is right, the specification talks about only one wildcard (*).

I double checked my code and noticed that the place where I found double wildcards (**) was in a Spring SimpleUrlHandlerMapping bean.

In that case, it makes sense. As per the class doc, it uses AntPathMatcher, which states:

The mapping matches URLs using the following rules: ? matches one character * matches zero or more characters ** matches zero or more 'directories' in a path

fegemo
  • 2,475
  • 6
  • 20
  • 40
  • I think only first `*` will be used as wildcard, and the 2nd `*` is treated as `literal *`. AFAIK, `*` is only treated as wildcard when used besides `/`. – Rohit Jain Dec 12 '12 at 15:55
  • 1
    Where have you seen this usage? – Thorbjørn Ravn Andersen Dec 12 '12 at 16:09
  • 1
    See http://stackoverflow.com/questions/15479213, the double asterisk can be use in a `` configuration and is defined in Spring's [AntPathMatcher](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html) – andyb Dec 03 '15 at 13:59

3 Answers3

34

Section 11.2 of servlet specification (version 2.5) states the following:

In the Web application deployment descriptor, the following syntax is used to define mappings:

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
  • A string beginning with a ‘*.’ prefix is used as an extension mapping.
  • A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  • All other strings are used for exact matches only.

So I guess the second variant (**) doesn't make sense.

P.S. I've just tried to set up such mapping and it seems that only this exact url /shop/** will be matched (Tomcat 6.0.32).

Andrew Logvinov
  • 21,181
  • 6
  • 52
  • 54
8

The Servlet specification itself (Version 3.0, Chapter 12.2) defines only two kinds of wildcard mapping:

  • If the URL pattern ends with "/*", it matches all requests to the preceding path.
  • If the URL pattern starts with "*.", it matches all requests to any resource ending with the following extension.

E.g. "/foo/*" will match all requests for URLs starting with "http://server/context/foo/" and "*.jsp" will match all requests for URL ending with ".jsp".

Following the specification, the empty string ("") and a single slash ("/") have specific meanings. "All other strings are used for exact matches only."

Following the specification strictly, your second example is not a wildcard pattern at all, but should only match "/shop/**" exactly. Most Servlet containers are however less strict and allow using the * wildcard at arbitrary locations or offer even more complex pattern matching options.

Community
  • 1
  • 1
jarnbjo
  • 33,923
  • 7
  • 70
  • 94
4

No where in specification talking about second case.

As per servlet specification 12.2

A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping

All other strings are used for exact matches only.

As per specification second would be considered for exact match only. It may vary based on server providers.

Roman C
  • 49,761
  • 33
  • 66
  • 176
kosa
  • 65,990
  • 13
  • 130
  • 167