In Java Web development, powerful filters are often used. After all, they can bring us great convenience, but we need to know in detail about the filtered resources. configuration Information in XML. This is summarized based on several common situations:
1. To map and filter all resources in an 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>
</Filter-mapping>
2. filter file resources of the specified type
<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>
<Url-pattern> *. html </url-pattern> to filter jsp, Set *. html to *. jsp, but note that there is no slash (/).
To filter multiple types of resources at the same time:
<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>
</Filter-mapping>
3. filter 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>
</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 specified files
<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>
Note whether the slash "/" exists.