Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

java - What does the double wildcard (*) on a servlet mapping url-pattern mean?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...