Servlet mapping with multiple (two) wildcards separated by slash

Sai picture Sai · Jun 1, 2013 · Viewed 10.4k times · Source

I am trying to map a servlet pattern that matches both

/server/abcDef/1432124/adfadfasdfa 

and

/server/abcDef/abcd/12345

The values '1432124' and 'abcd' are not fixed and could be a multitude of values. So essentially I need to match against /abcDef/*/* -- only the abcDef is fixed.

Is there a way for me to map this? Really I am looking for something like the following:

<servlet-mapping>
    <servlet-name>abcDefServlet</servlet-name>
    <url-pattern>/server/abcDef/*/*</url-pattern>
</servlet-mapping>

Answer

Cassio picture Cassio · Jun 1, 2013

According to the Servlet Specification, URL patterns ending with "/*" will match all requests to the preceding path. So, in the way you were doing it, you'd have to enter the following url to get to abcDefServlet:

http://myapp.com/server/abcDef/*/<wildcard>

What you can do though is add multiple URL patterns in one servlet mapping. E.g:

<servlet-mapping>
   <servlet-name>abcDefServlet</servlet-name>
   <url-pattern>/server/abcDef/1432124/*</url-pattern>
   <url-pattern>/server/abcDef/abcd/*</url-pattern>
</servlet-mapping>

Update:

Since 1432124 and abcd are not fixed values, you can safely add the following mapping:

<servlet-mapping>
   <servlet-name>abcDefServlet</servlet-name>
   <url-pattern>/server/abcDef/*</url-pattern>
</servlet-mapping>

And then treat whatever values that come after abcDef inside the servlet itself, with the following function:

req.getPathInfo()