Servlet Filter Learning

Source: Internet
Author: User

Recently, I have been studying CAS. the Servlet Filter in CAS is not very familiar with it. So I spent some time learning this part of knowledge and divided it into the following parts. 1. What is the Servlet Filter? What are the functions and usage of Servlet Filter? 2. Servlet Filter sequence considerations Part 1: What is the Servlet Filter? JAVA Doc API as below http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. filters perform filtering in the doFilter method. every Filter has access to a FilterConfig object from which it can obtain its initialization parameter S, and a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks. filters are configured in the deployment descriptor of a web application. examples that have been identified for this design are: Authentication FiltersLogging and Auditing FiltersImage conversion FiltersData compression FiltersEncryption filterencryption enizing FiltersFilters that trigg Er resource access eventsXSL/T filtersMime-type chain FilterSince: Servlet 2.3 read the JAVA Doc explanation and use filter to modify the content in request, response, session, and application. After learning about it, I started to perform some tests on Tomcat 6. Some of them are from the Internet. Example 1. javax. servlet. filter has two important methods: public void init (FilterConfig config) public void doFilter (ServletRequest request, ServletResponseresponse, FilterChainchain). The init method is used for initialization, the doFilter method is used to perform real filtering operations. 2. the deployment method of Filter is on the web. the filter element must be declared in xml and the url to be processed is allocated as follows: <filter> <filter-name> Filter1 </filter-name> <filter-class> com. test. filter1 </filter-class> </filter> <filter-name> Filter2 </filter-name> <filter-class> com. test. filter2 </filter-class> </filter> <filter-mapping> <filter-name> Filter1 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <filter-name> Filter2 </filter-name> <ur L-pattern>/* </url-pattern> </filter-mapping> Part 2: Considerations for Servlet Filter sequence 1. the execution sequence of the doFilter function. When a url request matches multiple filters, the application server organizes multiple filters into a FilterChain, the execution sequence of this chain is determined by the <filter-mapping> element on the web. the order in the xml file is determined. The doFilter function can Filter requests or responses. After filtering the requests, it will call the doFilter function of FilterChain to let the next Filter in FilterChain Filter the requests. The Filter that filters the request first filters the request. The following is a simple example. Public void doFilter (ServletRequest request, ServletResponseresponse, FilterChain chain) throws IOException, ServletException {system. out. println ("filter request here ----"); // todo: chain. doFilter (request, response); system. out. println ("filter response here ----"); // todo:} 2. the execution sequence of the init function 1 ). the init method of filter is loaded during container initialization. The first loading container is executed in a random order, and the next loading order is subject to the first loading order. 2 ). the doFilter method of filter is executed when the url is requested. If multiple filters match, the method is executed in the <filter-mapping> Order (provided that the doFilter method in the doFilter method finally calls the doFilter method of FilterChain, this method continues to execute the next filter. If no filter is added, the following filter is not executed.) 3 ). the init method of serlvet. if the web. if the <load-on-startup> attribute is configured in xml, the init method of all servlets is loaded in the order from small to large during Tomcat initialization. B. If the <load-on-startup> attribute is not configured, the container is not loaded during initialization. Load the url when the request matches, and load only the init method of the last servlet. Other servlets are not loaded. 4). servlet's doGet and doPost Methods: load when the request matches the url path, and only load the method of the last servlet, other methods are not loaded. 5). If the filter and servlet both exist and the container initialization must be loaded, load the filter before loading the servlet init method. 6 ). if the requested url matches both the filter and servlet, And the servlet init method is not loaded in container initialization, load the init method of the last servlet of the matching servlet first, then, the filter method is executed in order, and the matching servlet method is executed.

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.