我有一个与 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>
如果我访问 http://localhost:<port>/MyApp/HelloWorld
servlet HelloWorldServlet
被调用。
我还希望我的 serverlet 响应 http://localhost:<port>/MyApp/HelloWorld/
.我怎样才能达到这个效果?我正在使用 NetBeans 进行开发,但它不允许我放置以 /
结尾的模式.
最佳答案
在您的 <url-pattern>
添加通配符后
<url-pattern>/HelloWorld/*</url-pattern>
您可以使用 HttpServletRequest.getPathInfo()
获取与 URL 关联的额外路径.
例如
http://localhost:<port>/MyApp/HelloWorld/one/
结果将是
/one/
来自 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.
关于java - Servlet 映射 : url-pattern for URLs with trailing slash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4377541/