调用过滤器的顺序是什么?
假设我的 web.xml 中有以下内容,
<filter-mapping>
<filter-name>F1</filter-name>
<url-pattern>/XYZ/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>F2</filter-name>
<url-pattern>/XYZ/abc.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>F3</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果请求以 /XYZ/abc.do 形式出现,则调用过滤器的顺序是什么?为什么?
Suppose I have following in my web.xml
<filter-mapping>
<filter-name>F1</filter-name>
<url-pattern>/XYZ/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>F2</filter-name>
<url-pattern>/XYZ/abc.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>F3</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
What will be the order in which the filters will be called if a request comes as /XYZ/abc.do and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们的映射按照在 web.xml 中定义的顺序
如果使用注释 (
@WebFilter
),则顺序 似乎未定义 - 您仍然必须在 web.xml 中声明
条目。In the order their mappings are defined in web.xml
If using annotations (
@WebFilter
) the order seems to be undefined - you still have to declare the<filter-mapping>
entries in web.xml.Servlet 规范 3.0:
简而言之:它们按照它们在 XML 文件中出现的顺序应用。如果您点击
和
绑定过滤器覆盖的 URL,就会变得很有趣,因为所有 URL 模式绑定过滤器都被覆盖在所有 servlet-name 绑定过滤器之前应用。我从来没有遇到过这种情况(根本没有看到任何 servlet-name 绑定过滤器),但我认为这可能会很令人困惑。Section 6.2.4 of the Servlet specification 3.0:
In short: they're applied in the order in which they appear in the XML file. It gets interesting if you hit an URL that's covered by both
<url-pattern>
and<servlet-name>
bound filters, because then all URL-pattern bound filters are applied before all servlet-name bound filters. I've never been in this situation (haven't seen any servlet-name bound filters at all), but I reckon it could be quite confusing.