JSP Filter Filter Configuration Filter Types Total Summary

  • 2021-12-11 08:34:27
  • OfStack

1. Configuration method

1 mapping filters all resources in the application


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/*</url-pattern> // Access the current host. All files under the current application root include all files under multi-level subdirectories. Note here * There is "before" / " 
</filter-mapping>

2 Filter the specified type file resource


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html</url-pattern> // Access the current host, all under the current application root directory html Documents, note: *.html Not before. " / " , Otherwise error 
</filter-mapping>

Where *. html to filter jsp, change *. html to *. jsp, but note that there is no "/" slash. If you want to filter multiple types of resources at the same time:

Method 1 url-pattern is written separately


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.jsp</url-pattern>// Access the current host, the current application root directory to all and all under subdirectories jsp Documents 
</filter-mapping>

Method 2 Merge url-pattern


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html;*.jsp</url-pattern>
</filter-mapping>//*.html;*.jsp Use a semicolon between two types ; Interval 

3 Filter all files in the specified directory


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/folder_name/*</url-pattern>// Access the current host, under the current application root directory folder_name All files in subdirectory (but multi-level subdirectory) 
</filter-mapping>

4 Filter the specified servlet


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <servlet-name>loggerservlet</servlet-name>
</filter-mapping>
<servlet>
  <servlet-name>loggerservlet</servlet-name>
  <servlet-class>myfilter.LoggerServlet</servlet-class>
</servlet>

5 Filter the specified file (that is, single 1 file)


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/simplefilter.html</url-pattern>
</filter-mapping>

6 Filter all files of the specified type in the specified directory

In this case, it cannot be configured once in web. xml, so it is necessary to combine the implementation class of filter

First configure in web. xml to filter all files in the specified directory


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/dir_name/*</url-pattern>
</filter-mapping>

Then, the requested uri or url is obtained from the doFilter method in the implementation class of filter, and whether the string of the specified file type is contained in the uri or url is judged, and whether to filter or not is decided


// Object requested by the user uri
String uri = request.getRequestURI();
if(uri.contains(".jsp")||uri.contains(".html"))
System.out.println(" Start filtering "+url);

7 Filter single 1 files of specified type in specified directory


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/dir_name/index.jsp</url-pattern>
</filter-mapping>

Pay attention to whether there is a slash "/"

2. Examples

Do not cache files


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html</url-pattern> // Access the current host, all under the current application root directory html Documents, note: *.html Not before. " / " , Otherwise error 
</filter-mapping>

0

Cache files for 1 week


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html</url-pattern> // Access the current host, all under the current application root directory html Documents, note: *.html Not before. " / " , Otherwise error 
</filter-mapping>

1

Custom filter:


<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html</url-pattern> // Access the current host, all under the current application root directory html Documents, note: *.html Not before. " / " , Otherwise error 
</filter-mapping>

2

Related articles: