Learn Notes _ filter details (filter Javaweb One of the three major components)

Source: Internet
Author: User

Filter Details

1 life cycle of filters

We have learned the life cycle of the servlet, so the life cycle of the filter is no more difficult!

(l) Init (filterconfig): The filter instance is created when the server is started, and each type of filter creates only one instance and is no longer created! After the filter instance is created, the init () method is called immediately to complete the initialization work, which is only executed once;

(l) doFilter (servletrequest req,servletresponse res,filterchain chain): This method will be accessed every time the user accesses the target resource (<url->pattern >index.jsp</url-pattern>) ", if you need to" release ", then you need to call Filterchain's doFilter (ServletRequest, Servletresponse) method (that is, the function must be written to square, such as the Dofilter function above ), if the Filterchain Dofilter () method is not called, then the target resource will not be executed;

(L) Destroy (): The server will put the filter into the cache after it has been created and will not normally destroy it. The filter object is typically destroyed when the server shuts down, and the server invokes the Destory () method of the filter object before destroying the filter object.  

2 Filterconfig

As you've seen, the parameter type of the init () method in the filter interface is the filterconfig type. It functions like servletconfig and corresponds to the configuration information in the Web. xml file. The following is a description of Filterconfig features:

(l) ServletContext Getservletcontext (): Method of obtaining ServletContext;

(l) String getfiltername (): Gets the configuration name of the filter and corresponds to the <filter-name> element;

(l) string Getinitparameter (string name): Gets the initialization configuration of the filter, corresponding to the <init-param> element;

(L) enumeration getinitparameternames (): Gets the name of all initialization parameters.   

3 Filterchain

The parameter of the DoFilter () method has a parameter of type Filterchain, and it has only one method: DoFilter (Servletrequest,servletresponse).

before we say Dofilter () method release, let request flow access to target resources! But this is not strict , actually call this method means, " I (current filter)" release, but does not mean that other people (other filters) also released.

That is, a target resource, may have deployed a number of filters , as you go to Beijing on the road there are multiple robbers (filter), and the first gang of people released, but does not mean that the second gang of people also released, so call Filterchain class of Dofilter ( ) method represents the Dofilter () method that executes the next filter, or executes the target resource!

If the current filter is the last filter, then calling the Chain.dofilter () method means executing the target resource instead of the last filter, then Chain.dofilter () represents the DoFilter () that executes the next filter Method.  

4 + filter Execution order

A target resource can specify multiple filters, and the order in which the filters are executed is the order of deployment in the Web. xml file:

<filter> <filter-name>MyFilter1[Cui 1] </filter-name> [Cui 1] because MyFilter1 is configured in front, the MyFilter1 Dofilter () method is executed first. <filter-class>Cn.itcast.filter.MyFilter1</filter-class> </filter>
<filter-mapping> <filter-name>MyFilter1</filter-name> <url-pattern>/index.jsp</url-pattern> </filter-mapping>
<filter> <filter-name>MyFilter2</filter-name> <filter-class>Cn.itcast.filter.MyFilter2</filter-class> </filter>
<filter-mapping> <filter-name>MyFilter2</filter-name> <url-pattern>/index.jsp</url-pattern> </filter-mapping>
public class MyFilter1 extends HTTPFilter {public void DoFilter (HttpServletRequest request, HttpServletResponse Respon SE, Filterchain chain) throws IOException, servletexception {System.out.println ("filter1 start ...");Chain.dofilter (request, response);//release, execute MyFilter2 's DoFilter () methodSystem.out.println ("Filter1 end ..."); }}public class MyFilter2 extends HTTPFilter {public void DoFilter (HttpServletRequest request, HttpServletResponse resp Onse, Filterchain chain) throws IOException, servletexception {System.out.println ("filter2 start ...");Chain.dofilter (request, response);//release, execute target resourceSystem.out.println ("Filter2 end ..."); }} <body> This is my JSP page. <br>   


When a user accesses the index.jsp page, the output is as follows:

Filter1 start ...

Filter2 start ...

index.jsp

Filter2 End ...

Filter1 End ...

Learn Notes _ filter details (filter Javaweb One of the three major components)

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.