Servlet映射:具有尾部斜杠的URL的url-pattern [英] Servlet mapping: url-pattern for URLs with trailing slash
问题描述
我遇到了与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.
我也希望我的servlet能够回复 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>
<url-pattern>/HelloWorld/*</url-pattern>
您可以使用HttpServletRequest.getPathInfo()
。
You can get the extra path associated with the URL by using HttpServletRequest.getPathInfo()
.
例如
http:// localhost:< port> / MyApp / HelloWorld / one /
结果将是
/one/
来自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屋!