Filterr Filters for Web containers

Source: Internet
Author: User

Filterr filter ************* for Web containers


Configuration in the 1--web.xml file (note the request name)
The filter is called before the servlet, and multiple filter configurations are performed in the order in which the filter-mapping is configured, such as: Two filter with the request name and servlet with the requested name
When the client makes a request for the request name, it first invokes the configured filter, calls the latter filter, and finally calls the servlet, if there is a JSP with the same name as the request, then this is the JSP that is called at the end of the
Thus, we can design a simple IP address filter, according to the user's IP address for the site access control.


The request between 2--filter and filter, response can be deferred, that is, the request of the first filter set the property, then the second filter can be obtained, the final servlet can be obtained;
(Of course, you need to use the filter chain to pass the OH)
Three ways to 3--filter filters:
Initialize the init () Web container to start, call this method when initializing filter to initialize the parameters used by the filter, or other required parameters, for example: Initialize Filterconfig
Filter Processing DoFilter ()The Dofilter (Request,response) method of the filter chain Filterchain can be called after the response processing of the request continues to invoke the following filter, servlet, or JSP page
Destroy Destroy ()The user frees the resources that the filter occupies


The 4--filterconfig interface has four methods defined, each of which can be obtained
Getfiltername () returns the name of the filter defined in the Web. XML deployment File
Getservletcontext () returns the servlet context where the caller is located ServletContext
Getinitparameter (Java.lang.Stringname) returns the string form of the filter initialization parameter value, returning the Null.name initialization parameter name when the argument does not exist
Getinitparameternames () returns the filter all initialization parameter names in enumeration form, and returns null if there are no initialization parameters.


5--Create filter steps:
Step One: Create a class that implements the filter interface and implement its three methods: Init, Dofilter, and destroy

Step Two: Write the filtering behavior in the Dofilter method. Three of these parameters are ServletRequest, Servletresponse, and Filterchain, respectively,
The first two can be strongly converted to HttpServletRequest and HttpServletResponse use,
The third parameter is used to activate the next related filter.
If no other filter is associated with a servlet or JSP page, the servlet or JSP page is activated.

Step three: Configure Web. xml

Step four: Request a Test

Filterr Filters for Web containers

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.