使用斜杠分隔的多个(两个)通配符的 Servlet 映射 [英] Servlet mapping with multiple (two) wildcards separated by slash
问题描述
我正在尝试映射一个同时匹配两个
I am trying to map a servlet pattern that matches both
/server/abcDef/1432124/adfadfasdfa
和
/server/abcDef/abcd/12345
值 '1432124' 和 'abcd' 不是固定的,可能是多个值.所以基本上我需要匹配 /abcDef/*/*
—— 只有 abcDef 是固定的.
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>
推荐答案
根据 Servlet 规范,以/*"结尾的 URL 模式将匹配到前面路径的所有请求.因此,按照您的操作方式,您必须输入以下 url 才能访问 abcDefServlet:
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>
您可以做的是在一个 servlet 映射中添加多个 URL 模式.例如:
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>
<小时>
更新:
由于 1432124
和 abcd
不是固定值,您可以安全地添加以下映射:
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>
然后使用以下函数处理 servlet 本身内 abcDef
之后的任何值:
And then treat whatever values that come after abcDef
inside the servlet itself, with the following function:
req.getPathInfo()
这篇关于使用斜杠分隔的多个(两个)通配符的 Servlet 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!