您如何在JSP中定义多个过滤器?

您的Web应用程序可能会针对特定目的定义几个不同的过滤器。考虑一下,您定义了两个过滤器AuthenFilterLogFilter。剩下的过程将如上 ,除非您需要创建其他映射,如下 -

<filter>
   <filter-name>LogFilter</filter-name>
   <filter-class>LogFilter</filter-class>
   <init-param>
      <param-name>test-param</param-name>
      <param-value>Initialization Paramter</param-value>
   </init-param>
</filter>
<filter>
   <filter-name>AuthenFilter</filter-name>
   <filter-class>AuthenFilter</filter-class>
   <init-param>
      <param-name>test-param</param-name>
      <param-value>Initialization Paramter</param-value>
   </init-param>
</filter>
<filter-mapping>
   <filter-name>LogFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
   <filter-name>AuthenFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

筛选申请单

web.xml中过滤器映射元素的顺序确定了Web容器将过滤器应用于servlet或JSP的顺序。要颠倒过滤器的顺序,您只需要颠倒web.xml文件中的过滤器映射元素。

例如,上面的示例将首先应用LogFilter,然后将AuthenFilter应用到任何servlet或JSP;以下示例将颠倒顺序-

<filter-mapping>
   <filter-name>AuthenFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
   <filter-name>LogFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>