Javaweb three components--the operation mechanism understanding of the filter

Source: Internet
Author: User

Filter filters

This article focuses on practical and understanding.

I. The concept of a filter.

Lfilter is also known as a filter, which is the most practical technology in Servlet technology, Web developers through the filter technology, Web server management of all Web resources: such as JSP, Servlet, static picture files or static HTML files, etc. to intercept, So that some special functions can be realized. For example, the implementation of URL-level access control, filtering sensitive words, compressed response information and other advanced features.Two. The operation mechanism of the filter. The Web project without filter is running as follows: plus the web run mechanism of filter: This shows: the role of filter is green website, protect the website, realize access control and so on.three. How to implement a filter.
    • Write a class to implement the filter interface.
    • Configure Filter
  <!--Configure Filters-  <filter>  <!--filter name--  <filter-name>filterdemo1</ Filter-name>  <!--The full path of the filter class--  <filter-class>cn.itcast.filter.filterdemo1</ filter-class>  </filter>    <!--configuration Filter mapping  --<filter-mapping> <!--filter Name-- >  <filter-name>FilterDemo1</filter-name>  <!--filter path--  <url-pattern>/ *</url-pattern>  </filter-mapping>

  

<filter-name> is used to specify a name for the filter, and the content of the element cannot be empty. The <filter-class> element is used to specify the full qualified class name of the filter.

The <init-param> element is used to specify the initialization parameters for the filter, its child elements <param-name> the name of the specified parameter,<param-value> the value of the specified parameter. In the filter, you can use the Filterconfig interface object to access the initialization parameters

Configuration of <url-pattern> Tags:

* Full path match:/start. such as/aa/bb/aa/bb ...

* Directory matching:/start. End With *. * */aa/*/aa/bb/*

* Extension matches: Cannot start with/. such as *.jsp *.do *.action

After the filter is configured, the filter path matching its own path is automatically searched before the server invokes the servlet, and then the filter is executed. Without a match, the servlet is called directly.

Four. The concept of the filter chain.

Typically, after a client requests a server, the server calls the servlet before executing a set of filters (multiple filters), then this set of filters is called a filter chain.

Each filter implements a specific function, and a filter detects multiple servlets. (Match several, detect several).

The order of execution in a set of filters is related to the order of <filter-mapping> configuration.

When the first filter's Dofilter method is called, the Web server creates a Filterchain object that represents the filter chain to the method. In the Dofilter method, if the developer calls the Dofilter method of the Filterchain object, the Web server checks to see if there is a filter in the Filterchain object, and if so, the 2nd filter, if not, The target resource is called.

five. The life cycle of the filter. Life cycle Graphs: three ways:linit (Filterconfig filterconfig) throws Servletexception:Like the servlet program we wrote, the creation and destruction of the filter is the responsibility of the Web server.    When the Web application starts, the Web server creates an instance object of filter and invokes its Init method for initialization (note: The filter object is created only once and the Init party executes only once). The Filterconfig object that represents the current filter configuration information is available to the developer through the parameters of the Init method. (Filterconfig object see next page ppt) Ldofilter (servletrequest,servletresponse,filterchain) Every time the filter is intercepted it is performed in the actual development method in which the parameter request and response are typically converted to HttpServletRequest and HttpServletResponse types to operate.DoFilter (servletrequest,servletresponse,filterchain):Each filter is intercepted and will be executed. In the actual development of the method, the parameter request and the response are usually converted to HttpServletRequest and HttpServletResponse types to operate.Destroy ():Called before the Web container unloads the Filter object.Six. The operation mechanism of the filter chain. Analyze the operating mechanism:
    • The dofilter inside the filter is the Dofilter (Request,response) method of the filter instance.
    • This chain of responsibility is run in sequence: Code 1, Code 3, code 5,servelt, Web resources, code 6, code 4, code 2; It is because of its tail-to-echo mechanism, it is called the chain of responsibility.
    • In filter, if you do not call the Chain.dofilter (Request,response) method, it means that the request is rejected and the destination path is returned.

Seven. Obtain the configuration information for the filter in the Web. xml file.

  The calling object of the method:

The FILTERCONIFG object in the init (Filterconfig filterconfig) method.

  Method:

Getfiltername (): Get Filter Name

Getinitparameter (String name): Gets the initialization parameters of the filter

Getinitparameternames (): Gets the name of all initialization parameters for the filter

Getservletcontext (): Get ServletContext Object

  

Javaweb three components--the operation mechanism understanding of the filter

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.