java - Servlet 映射 : url-pattern for URLs with trailing slash

标签 java tomcat servlets netbeans 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>

如果我访问 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/

相关文章:

java - java 7中文件系统的含义是什么?

java - 使用 JavaMail 解析 MIME 消息

java - adb连接错误:EOF (new in android )

java - Mybatis几个jndi名称

java - Tomcat : PreResources tag not loading jars

java - 在不重启 JVM 的情况下在 JAVA 中重新加载 Kerberos 配置

java - 在 TomCat 服务器上使用 JavaServlets 的 Cronjob

java - 多次点击 JButton 按下

java - JSP 页面显示 bean 为空值

java - 如何在 Eclipse 中删除模板