java 使用斜杠分隔的多个(两个)通配符的 Servlet 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16867674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:12:30  来源:igfitidea点击:

Servlet mapping with multiple (two) wildcards separated by slash

javaservletsweb.xmlurl-pattern

提问by Sai

I am trying to map a servlet pattern that matches both

我正在尝试映射与两者都匹配的 servlet 模式

/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 abcDefis fixed.

值 '1432124' 和 'abcd' 不是固定的,可以是多个值。所以基本上我需要匹配/abcDef/*/*- 只有abcDef是固定的。

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>

回答by Cassio

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 urlto get to abcDefServlet:

根据Servlet 规范,以“/*”结尾的 URL 模式会将所有请求匹配到前面的路径。因此,按照您的操作方式,您必须输入以下URL才能访问 abcDefServlet:

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

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

您可以做的是在一个 servlet 映射中添加多个 URL 模式。例如:

<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 1432124and abcdare not fixed values, you can safely add the following mapping:

由于1432124abcd不是固定值,您可以安全地添加以下映射:

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

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

然后abcDef使用以下函数处理 servlet 内部后面的任何值:

req.getPathInfo()