7

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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sai
  • 3,819
  • 1
  • 25
  • 28

1 Answers1

11

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()
informatik01
  • 16,038
  • 10
  • 74
  • 104
Cassio
  • 2,912
  • 2
  • 20
  • 21
  • Sorry i should have mentioned that '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. I will edit my question to be very clear. – Sai Jun 01 '13 at 01:41
  • @Sai: Ok. I have now updated the answer as well. Please check it. – Cassio Jun 01 '13 at 13:46