Servlet mapping with multiple (two) wildcards separated by slash

巧了我就是萌 提交于 2019-12-23 08:07:40

问题


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>

回答1:


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


来源:https://stackoverflow.com/questions/16867674/servlet-mapping-with-multiple-two-wildcards-separated-by-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!