Servlet 过滤器 url 映射
如何将过滤器映射到 URL 的根?我正在使用 Tomcat 7.0.2 并将应用程序部署为 ROOT.war。欢迎页面是sign_in.xhtml。每当客户端发送对站点根目录(即仅域名)的请求时,或者当客户端请求sign_in.xhtml时,我想运行过滤器。这是我到目前为止所得到的:
<filter>
<filter-name>My filter</filter-name>
<filter-class>com.myApp.myFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>My filter</filter-name>
<url-pattern>/sign_in.xhtml</url-pattern>
</filter-mapping>
直接请求sign_in.xhtml,成功调用过滤器,但我不确定如何获取根请求来调用过滤器。根据 Servlet 规范(版本 3.0),
<url-pattern>/</url-pattern>
映射到默认 servlet,空字符串映射到根。以下是规范中的相关部分:
“空字符串 ("") 是一种特殊的 URL 模式,它精确映射到 应用程序的上下文根,即 http://host:port// 形式的请求。在本例中,路径信息为“/”,servlet 路径和上下文路径为 空字符串 (“”)。”
但是,以下两种 url 模式都会导致 Tomcat 抛出异常。
<url-pattern></url-pattern>
<url-pattern>""</url-pattern>
如果有人能对此有所了解,我将非常感激。谢谢。
安德鲁
How can a filter be mapped to the root of a URL? I'm using Tomcat 7.0.2 and deploying an application as ROOT.war. The welcome page is sign_in.xhtml. I would like to run a filter whenever the client sends a request for the root of the site (i.e. the domain name only), or when the the client requests sign_in.xhtml. Here is what I have so far:
<filter>
<filter-name>My filter</filter-name>
<filter-class>com.myApp.myFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>My filter</filter-name>
<url-pattern>/sign_in.xhtml</url-pattern>
</filter-mapping>
Requests for sign_in.xhtml directly, successfully invoke the filter, but I'm not sure how to get requests for the root to invoke the filter. According to the Servlet spec (version 3.0)
<url-pattern>/</url-pattern>
maps to the default servlet, and an empty string maps to the root. Here's the relevant section from the spec:
"The empty string ("") is a special URL pattern that exactly maps to the
application's context root, i.e., requests of the form http://host:port//. In this case the path info is ’/’ and the servlet path and context path is
empty string (““)."
However, both of the following url patterns cause Tomcat to throw an exception.
<url-pattern></url-pattern>
<url-pattern>""</url-pattern>
I would really appreciate it if someone could shed some light on this. Thank You.
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
/
应该适用于根上的请求。你尝试过吗?如果您的目的更多是过滤所有请求,那么您应该使用
/*
。更新:为了排除其中一个,我在 Tomcat 7 上测试了
/
的url-pattern
(同时使用web.xml< /code> 和
@WebFilter(urlPatterns={"/"})
) ,它确实没有按预期工作。然而,/
的url-pattern
在 Tomcat 6 (Servlet 2.5) 和 Glassfish v3 (Servlet 3.0) 上按预期工作。我怀疑 Tomcat 7 中存在错误,因此我报告了问题 49914关于这个。The
<url-pattern>/</url-pattern>
should work for requests on the root. Did you try it?If your intent is more to filter all requests, then you should use
<url-pattern>/*</url-pattern>
.Update: To exclude one and other, I tested the
url-pattern
of/
at Tomcat 7 (using bothweb.xml
and@WebFilter(urlPatterns={"/"})
) and it indeed didn't work as expected. Theurl-pattern
of/
however works as expected on Tomcat 6 (Servlet 2.5) and also on Glassfish v3 (Servlet 3.0). I suspect a bug in Tomcat 7, so I've reported issue 49914 about this.