Servlet Filters Filter

Source: Internet
Author: User

The filter is a Java class that implements the Javax.servlet.Filter interface. The Javax.servlet.Filter interface defines three methods:

Here is the code filter for all

 Packagefilter;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classCharacterencodingfilterImplementsFilter {filterconfig config;  Public voiddestroy () {//TODO auto-generated Method Stub            }     Public voidDoFilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2)throwsIOException, servletexception {String characterencoding=config.getinitparameter ("Characterencoding");        Arg0.setcharacterencoding (characterencoding);        Arg1.setcharacterencoding (characterencoding); Arg1.setcontenttype ("Text/html;charset=" +characterencoding);    Arg2.dofilter (arg0, arg1); }     Public voidInit (Filterconfig arg0)throwsservletexception {//TODO auto-generated Method Stub         This. config=arg0; }}

Web. XML configuration

  <Filter>      <Filter-name>Characterencodingfilter</Filter-name>      <Filter-class>Filter. Characterencodingfilter</Filter-class>      <Init-param>          <Param-name>Characterencoding</Param-name>          <Param-value>UTF-8</Param-value>      </Init-param>  </Filter>  <filter-mapping>      <Filter-name>Characterencodingfilter</Filter-name>      <Url-pattern>/*</Url-pattern>  </filter-mapping>

Using multiple filters

A WEB application can define several different filters based on specific purposes. Assume that you have defined two filters authenfilter and logfilter. You need to create a different mapping as described below, with the rest being roughly the same as explained above:

<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>
Application Order of filters

The order of the filter-mapping elements in Web. XML determines the order in which the filter is applied to the Servlet by the website container. To reverse the order of the filters, you only need to invert the filter-mapping element in the Web. xml file.

For example, the above instance will first apply Logfilter and then apply Authenfilter, but the following instance will reverse the order:

<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>

Servlet Filters Filter

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.