Servlet 映射:带有尾部斜杠的 URL 的 url-pattern [英] Servlet mapping: url-pattern for URLs with trailing slash

查看:47
本文介绍了Servlet 映射:带有尾部斜杠的 URL 的 url-pattern的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem related to the servlet mapping. I have the following in 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>

如果我访问 http://localhost:<port>/MyApp/HelloWorld servlet HelloWorldServlet 被调用.

If I access to http://localhost:<port>/MyApp/HelloWorld the servlet HelloWorldServlet is called.

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

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

推荐答案

在您的

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

您可以使用 HttpServletRequest.getPathInfo().

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

例如

http://localhost:/MyApp/HelloWorld/one/

结果是

/one/

来自 JavaDoc:

From the JavaDoc:

返回任何额外的路径信息与客户端的 URL 相关联当它提出这个请求时发送.这额外的路径信息跟随servlet 路径但在查询之前字符串并以/"开头字符.

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.

这篇关于Servlet 映射:带有尾部斜杠的 URL 的 url-pattern的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆