Summary of filter configuration in JSP servlet)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.