Filter, filter
I. Filter
A filter is a component that adds functions to the request and Response of a web application. The filter can filter and filter the interaction information between the client and the target resource, and ultimately retain valid data information.
2. Life Cycle 2.1 of the filter is instantiated.
The web Container copies and creates an instance of the filter to instantiate the filter only once.
2.2 initialization.
Before filtering, the init () method is called to implement initialization only once.
2.3 Filter
Call the doFilter () method to implement specific functions of the filter.
4. Destroy
Call the destory () method to release the resources occupied by the filter.
3. Four Steps for filter application 3.1 filter development:
(1). Create a class to implement the Filter Interface
(2). Compile the filtering method in doFilter.
(3). Call the next filter or web Resource
(4). Configure the filter in the web. xml file.
3.2 Create a Filter class
Package demo. web. servlet; import java. io. IOException; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; public class CharacterEncodingFilter implements Filter {public void destroy () {} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {// set the request Encoding Method request. setCharacterEncoding ("UTF-8"); // you can call other filter chains to call web resources. doFilter (request, response); // sets the encoding method for the response. setCharacterEncoding ("UTF-8");} public void init (FilterConfig arg0) throws ServletException {}}
Create a MyFilter filter class.
Package demo. web. filter; import java. io. IOException; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; public class MyFilter implements Filter {public void destroy () {// TODO Auto-generated method stub} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {// TODO Auto-generated method stubrequest. setCharacterEncoding ("UTF-8"); System. out. println ("myFilter starts by calling doFilter"); chain. doFilter (request, response); response. setCharacterEncoding ("UTF-8"); System. out. println ("myFilter call doFilter end");} public void init (FilterConfig filterConfig) throws ServletException {// TODO Auto-generated method stub }}3.3 configure filter in web. xml
<Filter> <filter-name> CharacterEncodingFilter </filter-name> <filter-class> demo. web. filter. characterEncodingFilter </filter-class> </filter> <filter-mapping> <filter-name> CharacterEncodingFilter </filter-name> <url-pattern>/* </url-pattern> <! -- Filter all site information. The configuration is similar to servlet:/xx/* Or/xx --> </filter-mapping>
<Filter> <display-name> myFilter </display-name> <filter-name> myFilter </filter-name> <filter-class> demo. web. filter. myFilter </filter-class> </filter> <filter-mapping> <filter-name> myFilter </filter-name> <url-pattern>/* </url-pattern> <! -- Filter all site information --> <dispatcher> REQUEST </dispatcher> </filter-mapping>
<Dispatcher> specify the method in which the resource intercepted by the filter is called by the Servlet container. It can be one of REQUEST, INCLUDE, FORWARD, and ERROR. The default value is REQUEST. You can set multiple
<Dispatcher> A subelement is used to specify a Filter to intercept multiple resource calling methods.
<Dispatcher> values that can be set by sub-elements and their meanings:
1. REQUEST: When you directly access the page, the Web Container will call the filter. If the target resource is accessed through the include () or forward () method of RequestDispatcher, the filter will not be called.
2. INCLUDE: if the target resource is accessed through the include () method of RequestDispatcher, the filter will be called. In addition, the filter will not be called.
3. FORWARD: if the target resource is accessed through the forward () method of RequestDispatcher, the filter is called. In addition, the filter is not called.
4. ERROR: if the target resource is called through the declarative Exception Handling Mechanism, the filter will be called. In addition, the filter will not be called.
3.4 filter chain
Multiple filters can be developed in web applications. These filters are a Filter chain.
The web server depends on the Filter in the web. the registration order in the xml file determines which Filter to call first. When the doFilter method of the first Filter is called, the web server will create a FilterChain object that represents the Filter chain for passing
To this method. In the doFilter method, if a developer calls the doFilter method of the FilterChain object, the web server checks whether there is a filter in the FilterChain object. If there is a filter, it calls 2nd filters.
Yes, the target resource is called.
Create a servlet, which has been created in the previous servlet article
Call http: // localhost: 8080/servletDemo/myServlet in the browser and Output
If you have run the result analysis, you can know that the filtering is based on the web. the configuration sequence in xml first calls servlet initialization, then calls the doFilter method of CharacterEncodingFilter, and runs the program to the chain. doFilter (req, resp );
The web. the Filter under the characterEncodingFilter configured in xml, that is, the doFilter () method in MyFilter is called. The servlet doGet () method is called only after the nested Filter is executed;
Note: In this process, the servlet will only initialize once, And the servlet will not call the init () method of the Filter after initialization. The Filter is located under the javax. servlet. filter package.