Search code examples
javaservletsweb.xmlurl-pattern

Servlet mapping with multiple (two) wildcards separated by slash


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>

Solution

  • 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()