Filter Introduction
Filter can be considered as a "variant" of the servlet, it is mainly used to preprocess user requests, or httpservletresponse, is a typical processing chain. It differs from the servlet in that it does not generate a response directly to the user. The complete process is: Filter to the user request preprocessing, then the request to the servlet to process and generate a response, and finally filter to the server response after processing.
Filter has the following several uses.
Intercept the customer's httpservletrequest before HttpServletRequest arrives in the servlet.
Check httpservletrequest as needed, or modify httpservletrequest headers and data.
Intercept HttpServletResponse before HttpServletResponse arrives at the client.
Check httpservletresponse as needed, or modify HttpServletResponse headers and data.
There are several kinds of filter.
The user-authorized Filter:filter is responsible for checking the user's request and filtering the user's illegal request according to the request.
Log filter: Details specific user requests are logged.
Filter for decoding: includes decoding of requests for non-standard encodings.
Can change the XSLT filter of XML content.
Filter is responsible for intercepting multiple requests or responses, and a request or response can be intercepted by multiple requests.
Creating a filter takes only two steps:
Build filter processing class;
Configure filter in the Web.xml file.
6.filter: Filter Configuration: Associate a name with a class that implements the Javaxs.servlet.Filter interface
In a Web application, you can develop and write multiple filter sets called a filter chain.
The Web server decides which filter to call first, based on the order in which filter is registered in the Web.xml file.
When the Dofilter method of the first filter is invoked, the Web server creates a Filterchain object that represents the filter chain to pass to the method.
In the Dofilter method, if the developer calls the Dofilter method of the Filterchain object, the Web server checks to see if there is any filter in the Filterchain object.
If so, the 2nd filter is invoked, and if not, the target resource is invoked.
<filter>
<filter-name>testFitler</filter-name>
<filter-class> org.test.testfiter</filter-class>
<init-param>
<param-name>word_file</param-name >
<param-value>/WEB-INF/word.txt</param-value>
</init-param>
</filter>
<filter-name> to specify a name for the filter, the contents of the element cannot be empty.
The <filter-class> element is used to specify the complete qualified class name for the filter.
The <init-param> element is used to specify the initialization parameter for the filter, its child element <param-name> the name of the specified parameter,<param-value> the value of the specified parameter.
In a filter, you can use the Filterconfig interface object to access initialization parameters.
Filterconfig Interface:
When the user configures filter, you can use <init-param> to configure some initialization parameters for the filter.
When the Web container instantiates the filter object and calls its Init method, the Filterconfig object that encapsulates the filter initialization parameter is passed in.
So when the developer writes the filter, by filterconfig the object's method, it is available:
String getfiltername (): Gets the name of the filter.
String Getinitparameter (string name): Returns the value of the initialization parameter that specifies the name in the deployment description. Returns null if it does not exist.
Enumeration Getinitparameternames (): An enumeration collection that returns the names of all initialization parameters for a filter.
Public ServletContext Getservletcontext (): Returns a reference to the Servlet context object.
The <filter-mapping> element is used to set a resource that the filter is responsible for intercepting.
A filter-blocking resource can be specified in two ways: the Servlet name and the request path for resource access
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/
The <filter-name> child element is used to set the registration name of the filter. The value must be the name of the filter declared in the <filter> element
<url-pattern> set the request path blocked by filter (filter associated URL style)
<servlet-name> Specifies the servlet name that the filter intercepts.
<dispatcher> specifies how the resource intercepted by the filter is invoked by the Servlet container, which can be one of the Request,include,forward and the error,
The default request. Users can set multiple <dispatcher> child elements to specify how the Filter will intercept multiple calls to the resource.
The value that the <dispatcher> child element can set and its meaning:
REQUEST: The Web container will invoke the filter when the user accesses the page directly. If the target resource is accessed through the requestdispatcher include () or forward () method, then the filter is not invoked.
Include: If the target resource is accessed through the RequestDispatcher include () method, the filter is invoked. In addition, the filter is not invoked.
FORWARD: If the target resource is accessed through the RequestDispatcher FORWARD () method, then the filter will be invoked, and the filter will not be invoked.
ERROR: If the target resource is invoked through a declarative exception handling mechanism, the filter is invoked. In addition, filters are not invoked.
<filter>
<!--define the name of the filter. -->
<filter-name>URIFilter</filter-name>
<!--define the class name of the filter-->
<filter-class >org.cjj.filter.URIFilter</filter-class>
<init-param>
<param-name>encoding</ param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!--The two main child elements of the filter-mapping element Filter-name and Url-pattern. Defines the URL for the filter. Like the servlet-mapping below -- >
<filter-mapping>
<!--define the name of the filter, consistent with the filter name defined above-->
<filter-name> Urifilter</filter-name>
<!--ilter corresponds to the Rul, here is all the URL-->
<url-pattern>/*</ Url-pattern>
</filter-mapping>