The servlet filter is sandwiched between the user and the servlet, and can intercept and preprocess or interrupt {user-to-servlet requests or servlet-to-user responses}, but cannot handle and respond to user requests by itself.
The filter class must implement the three methods of the filter interface:
Init: Create an instance of each filter based on the configuration in Web. xml
DoFilter: Executed when the user requests the corresponding filtered URL
Destroy:web container unloading filter is performed
For the same request, you can have as many filter as the filter chain (chain) in the order defined by the filter in Web. Xml.
The first declaration in Web. XML is first executed doFilter, there is a chain.dofilter (request, response) in DoFilter, and the DoFilter method of the next filter in the filter chain continues. The DoFilter method is roughly as follows: the pre-processing block ====chain.dofilter (request, response); ===== post-processing. Therefore, after the declaration of the Dofilter method will be done first.
<Filter> <Filter-name>Encodingfilter</Filter-name> <Filter-class>Org.springframework.web.filter.CharacterEncodingFilter</Filter-class> <Init-param> <Param-name>Encoding</Param-name> <Param-value>UTF-8</Param-value> </Init-param> <Init-param> <Param-name>Forceencoding</Param-name> <Param-value>True</Param-value> </Init-param> </Filter> <filter-mapping> <Filter-name>Encodingfilter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping>
The filter defined in Web. XML can execute the init parameter by setting the filter Init-param. Filter-mapping set the URL address for this filter
The servlet filter uses