Java filter (filter) 1

Source: Internet
Author: User

Introduction to filter:


The basic function of filter is to intercept the servlet container call servlet process, so as to implement some special functions before and after servlet Response Processing. In

 

Servlet APIs define three interface classes for developers to write filter programs: filter, filterchain, and filterconfig. The filter program implements the filter

 

The Java class of the interface, similar to the servlet program, is called by the servlet container and the filter program needs to be registered and set in the web. xml file to intercept

 

Resources: The filter program can intercept JSP, Servlet, static image files and static html files.

 

The basic working principle of filter:

When a filter is registered in Web. XML to intercept a servlet program, the filter becomes the servlet container and the servlet program.

 

A level on the communication line. The filter can block the requests sent by the servlet container to the servlet program and the requests sent by the servlet program back to the servlet container.

 

You can decide whether to pass the request to the servlet program, and whether to modify the request and the relevant information. In a web application, you can register multiple filters.

 

Each filter program can intercept one or more servlet programs. If multiple filters intercept the access process of a servlet program

 

When an access request to the servlet arrives, the Web Container combines these filters into a filter chain ). Interception sequence of each filter in the filter chain

 

It is consistent with the order in which they are mapped in the web. xml of the application.

 

Filter interface:

Init (filterconfig) throws servletexception: When a web application is started, the web server will

 

To create a registered filter instance object and store it in the memory of the server. After the Web Container creates a filter object instance, it immediately calls

 

Init method. The init method is only executed once in the filter lifecycle. When the Web Container calls the init method, it will pass a configuration containing the filter and the runtime environment

 

Filterconfig object (the usage of filterconfig is similar to that of servletconfig ). The filterconfig object can be used to obtain the servletcontext object and the deployment descriptor.

 

The initialization parameters of the configured filter. In this method, a servletexception exception can be thrown, notifying the container that the filter cannot work normally.

Destroy (): called before the Web Container uninstalls the filter object. This method is only executed once in the lifecycle of the filter. In this method, you can release the filter to use

 

.

Unlike servlet development, the filter interface does not have an implementation class for inheritance. To develop a filter, you can only implement the filter interface directly.

Dofilter (servletrequest request, servletresponse response, filterchain chain) throws

Java. Io. ioexception, servletexception:
The dofilter () method is similar to the Service () method of the servlet interface. When the client requests the target resource, the container will call the filter associated with the target resource.

 

Dofilter () method. The request and Response parameters are the requests and corresponding objects passed by the previous filter in the Web Container or filter chain.

 

The object of the Current Filter chain of the table. After a specific operation is completed, you can call the filterchain object's

 

The chain. dofilter (request, response) method can deliver the request to the next filter or target servlet program in the filter chain for processing.

 

The user can return response information, or use the forward () and include () Methods of requestdispatcher and the sendredirect () method of httpservletresponse.

 

Redirect requests to other resources. The request and Response parameters of this method are servletrequest and servletresponse. That is to say, the use of the filter is not dependent on

 

For specific protocols.

Filterchain interface:

Filterchain interface: the object of the Current Filter chain. Implemented by the container. The container uses its instance as a parameter to pass in the dofilter () method of the filter object. Filter object

 

Use a filterchain object to call the next filter in the filter chain. If the filter is the last filter in the chain, the target resource is called.

 

Dofilter (servletrequest request, servletresponse response) throws java. Io. ioexception: calling this method will make the next

 

Filters are called. If it is the last filter, the target resource is called. After implementing a filter, You need to register and set the information that can be intercepted in Web. xml.

 

Source. This can be done through the <filter> and <filter-mapping> elements.

 

 

The configuration method is very similar to servlet. The specific configuration code is as follows:

<Filter> <br/> <filter-Name> myfilterconfig </filter-Name> <br/> <filter-class> COM. myfilter. myfilterconfig </filter-class> <br/> <! -- Configure the initialization parameters of the current filter --> <br/> <init-param> <br/> <param-Name> username </param-Name> <br/> <Param -value> admin </param-value> <br/> </init-param> <br/> <param-Name> password </param-Name> <br/> <param-value> admin </param-value> <br/> </init-param> <br/> </filter> <br/> <filter-mapping> <br/> <filter-Name> myfilterconfig </filter-Name> <br/> <URL-pattern>/* </url-Pattern> <br/> </filter-mapping>

/* Indicates that all URLs must be filtered by this filter.

You can set multiple mappings for the same filter in the same web. xml file. If the same filter program appears multiple times in a filter chain, this filter program

 

The interception process will be executed multiple times.

 

 

Typical application of filter 1: Make the browser not cache the page Filter

Three HTTP response header fields can disable the browser from caching the current page. The sample code in servlet is as follows:

Response. setdateheader ("expires",-1 );

Response. setheader ("cache-control", "No-Cache ");

Response. setheader ("Pragma", "No-Cache ");

Not all browsers fully support the above three response headers, so it is best to use the above three response headers at the same time.

Typical application 2: character encoding filter

You can specify the encoding used by configuring the encoding parameter to handle Chinese characters in the HTML form request parameters.

The specific instance code is as follows:
Public void dofilter (servletrequest arg0, servletresponse arg1, filterchain arg2) throws ioexception, servletexception <br/>{< br/> arg0.setcharacterencoding ("UTF-8 "); </P> <p> arg2.dofilter (arg0, arg1); <br/>}

 

Related Article

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.