Java Servlet URL模式,以匹配以斜杠(“/”结尾的URL)

Java Servlet URL模式,以匹配以斜杠(“/”结尾的URL),java,servlets,web.xml,url-pattern,Java,Servlets,Web.xml,Url Pattern,我想指定一个Servlet URL模式,以匹配以斜杠(“/”)结尾且仅以斜杠结尾的URL。 我理解这种模式 /example/path/* /示例/路径/* 将匹配的URL为 http://example.com/example/path/ http://example.com/example/path/ 这似乎是可行的。然而,同样的模式也会匹配 http://example.com/example/path/a/ http://example.com/example/path/b/

我想指定一个Servlet URL模式,以匹配以斜杠(“/”)结尾且仅以斜杠结尾的URL。

我理解这种模式

/example/path/* /示例/路径/* 将匹配的URL为

http://example.com/example/path/ http://example.com/example/path/ 这似乎是可行的。然而,同样的模式也会匹配

http://example.com/example/path/a/ http://example.com/example/path/b/ http://example.com/example/path/c/ http://example.com/example/path/a/ http://example.com/example/path/b/ http://example.com/example/path/c/ 我只是在寻找一个只匹配
而不匹配
的URL模式,以此类推


澄清:不允许以斜杠结尾的URL模式。

很可能无法通过在web.xml中映射来实现这一点


您可以做的是将servlet映射到/mypath/*并通过request.getPathInto()检查/mypath/后面的部分。如果是“/”,请运行代码。如果不是,则返回404错误。

在NetBeans中,如果我转到web.xml文件上的servlet选项卡,IDE会抱怨“错误:URL模式不能以斜杠(/)结尾”。从照片上看,

httpurl        = "http://" hostport [ "/" hpath [ "?" search ]]
hpath          = hsegment *[ "/" hsegment ]

因此,是的,带有结尾斜杠的URI是无效的

你试过没有尾星吗?是的,我已经试过了。NetBeans告诉我,不允许使用以斜杠结尾的URL模式。