Java Servlet 映射:带有尾部斜杠的 URL 的 url-pattern

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4377541/
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-08-14 16:42:07  来源:igfitidea点击:

Servlet mapping: url-pattern for URLs with trailing slash

javatomcatservletsnetbeansweb.xml

提问by Gabriel Llamas

I have a problem related to the servlet mapping. I have the following in web.xml:

我有一个与 servlet 映射相关的问题。我在 web.xml 中有以下内容:

<servlet>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>test.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>

If I access to http://localhost:<port>/MyApp/HelloWorldthe servlet HelloWorldServletis called.

如果我访问http://localhost:<port>/MyApp/HelloWorldservletHelloWorldServlet被调用。

I also want my servelet to respond to http://localhost:<port>/MyApp/HelloWorld/. How can I achieve this effect? I'm developing with NetBeans but it does not allow me to put a pattern ended with /.

我还希望我的小服务器响应http://localhost:<port>/MyApp/HelloWorld/. 我怎样才能达到这个效果?我正在使用 NetBeans 进行开发,但它不允许我将模式以/.

采纳答案by Buhake Sindi

After you've added your wildcard on your <url-pattern>

在您添加通配符之后 <url-pattern>

<url-pattern>/HelloWorld/*</url-pattern>

You can get the extra path associated with the URL by using HttpServletRequest.getPathInfo().

您可以使用HttpServletRequest.getPathInfo().

E.g.

例如

http://localhost:<port>/MyApp/HelloWorld/one/

http://localhost:<port>/MyApp/HelloWorld/one/

The result will be

结果将是

/one/

From the JavaDoc:

来自 JavaDoc:

Returns any extra path information associated with the URL the client sent when it made this request. The extra path information follows the servlet path but precedes the query string and will start with a "/" character.

返回与客户端发出此请求时发送的 URL 关联的任何额外路径信息。额外的路径信息跟在 servlet 路径之后,但在查询字符串之前,并以“/”字符开头。

回答by Jonathan B

Use a wildcard. You can redirect all the traffic going to a specific URL to the same servlet. For example, you can add the following:

使用通配符。您可以将所有流向特定 URL 的流量重定向到同一个 servlet。例如,您可以添加以下内容:

<servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/HelloWorld/*</url-pattern>
</servlet-mapping>

This will redirect the URL with a slash to your original servlet.

这会将带有斜杠的 URL 重定向到您的原始 servlet。

One thought - this would redirect anything to this URL pattern to the servlet. If you want to have other URL's past this URL, you should create a servlet that will redirect to the correct URL (by looking at the URL specified). Alternatively, you could use a framework that provides mapping for you.

一个想法 - 这会将任何指向此 URL 模式的内容重定向到 servlet。如果您想让其他 URL 超过此 URL,您应该创建一个 servlet,它将重定向到正确的 URL(通过查看指定的 URL)。或者,您可以使用为您提供映射的框架。