zoukankan      html  css  js  c++  java
  • How to define Servlet filter order of execution using annotations

    If we define Servlet filters in web.xml, then the order of execution of the filters will be the same as the order in which they are defined in the web.xml.

    But, if we define the filters using annotation, what is the order of execution of filters and how can we determine the order of execution?

    You can indeed not define the filter execution order using @WebFilter annotation. However, to minimize the web.xml usage, it's sufficient to annotate all filters with just a filterName so that you don't need the <filter> definition, but just a <filter-mapping> definition in the desired order.

    For example,

    @WebFilter(filterName="filter1")publicclassFilter1implementsFilter{}@WebFilter(filterName="filter2")publicclassFilter2implementsFilter{}

    with in web.xml just this:

    <filter-mapping><filter-name>filter1</filter-name><url-pattern>/url1/*</url-pattern></filter-mapping><filter-mapping><filter-name>filter2</filter-name><url-pattern>/url2/*</url-pattern></filter-mapping>

    If you'd like to keep the URL pattern in @WebFilter, then you can just do like so,

    @WebFilter(filterName="filter1", urlPatterns="/url1/*")publicclassFilter1implementsFilter{}@WebFilter(filterName="filter2", urlPatterns="/url2/*")publicclassFilter2implementsFilter{}

    but you should still keep the <url-pattern> in web.xml, because it's required as per XSD, although it can be empty:

    <filter-mapping><filter-name>filter1</filter-name><url-pattern/></filter-mapping><filter-mapping><filter-name>filter2</filter-name><url-pattern/></filter-mapping>

    Regardless of the approach, this all will fail in Tomcat until version 7.0.28 because it chokes on presence of <filter-mapping> without <filter>. See also Using Tomcat, @WebFilter doesn't work with <filter-mapping> inside web.xml

  • 相关阅读:
    rman备份,恢复
    异步事件回调机制原理探索 (转)
    stock
    将知识变成你的技能点
    Tomcat的URL中文乱码解决以及传输优化
    李洪强iOS开发之-入门指南
    WebSocket 和 Socket 的区别
    李洪强iOS开发之-修改状态栏的字体的颜色
    关于UDID和UUID的区别
    李洪强iOS开发之
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3731332.html
Copyright © 2011-2022 走看看