Java Development Study Note 9: Filter

Source: Internet
Author: User

I. Concepts of filters
A filter is a layer of Web component between the customer and Servlet. It can intercept requests from the customer to web resources or the Servlet's response to the customer. A filter is a special Web component that can operate on the header attributes and body of requests and responses. Unlike General Web Components, filters do not directly generate Web responses. Several filters can point to the requests and request responses of a web resource.
The relationship between the filter and servlet is many-to-many. A filter can filter requests and responses of multiple servlets. A servlet can also be used by multiple filters, multiple filters filter a servlet to form a filter chain.
Filters provide the following functions:
1. Analyze Web requests and pre-process input data;
2. Early request and response blocking;
3. Change the request header information and data body based on the function;
4. Collaborate with other web resources.
However, these functions can be implemented in servlet. Why is there an extra filter. The answer is to consider programming structure design. Filters are mainly used for some universal operations. They are common and portable, such as security protection, Operation records, data encryption and decryption, and XML Conversion.

Ii. Usage of filters
The filters include javax. servlet. filter, javax. servlet. filterconfig, and filterchain APIs. The following program implements a filter interface.
Public final class simplefilter implements filter {
Private filterconfig fconfig = NULL;
Public void Init (filterconfig fconfig)
Throws servletexception {
This. fconfig = fconfig
}

Public void destroy (){
Fconfig = NULL;
}

Public void dofilter (servletrequest request, servletresponse response, filterchain chain)
Throws ioexception, servletexception {
...
Chain. dofilter (request, response );
...
}
}

Methods Init () and destroy () initialize and clear. When the Web Container receives a servlet or jsp request, if the servlet or JSP uses a filter, the Web Container will call the dofilter method for this filter. The dofilter method enables the filter to operate on requests and responses. The first two parameters of this method are request and response objects. The third parameter chain is the filter chain.
When a servlet or JSP has multiple filters, these filters form a filter chain that processes requests and responses in sequence. Call the dofilter method of filterchain in the dofilter method of a filter. The result is to activate the next filter in the filter chain. The end of the filter is servlet or JSP. If the current filter is the last one in the filter chain, the dofilter of the fitlerchain object calls servlet or JSP after execution. If any filter in the filter chain does not call the dofilter method of the filterchain object, the request is interrupted until that filter, and the target servlet or JSP will not receive the request. You can process resquest before the chain. dofilter (request, response) line, and then process the response returned by the servlet.

Iii. filter configuration instructions in the web. xml file
The following web. xml:
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app public
"-Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
...
<Filter>
<Icon> filter.jpg </icon>
<Filter-Name> simplefilter </filter-Name>
<Display-Name> asimplefilter </filter-Name>
<Description> the first filter example </description>
<Filter-class> fey. Filters. simplefilter </filter-class>
<Init-param>
<Param-Name> param1 </param-Name>
<Param-value> value1 </param-value>
</Init-param>
...
<Filter-mapping>
<Filter-Name> simplefilter </filter-Name>
<URL-pattern>/Header/* </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> simplefilter </filter-Name>
<URL-pattern>/headerinfo </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> simplefilter </filter-Name>
<Servlet-Name> simpleservlet </servlet-Name>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> simplefilter </filter-Name>
<Servlet-Name> formparam </servlet-Name>
</Filter-mapping>
...
</Web-app>
<Icon> filter.jpg </icon> specifies the icon used to display the filter in the programming tool;
<Filter-Name> simplefilter </filter-Name> defines the name of the filter;
<Display-Name> asimplefilter </filter-Name> defines the display name in the programming tool of the filter;
<Description> the first filter example </description> defines the annotation of the filter;
<Filter-class> fey. Filters. simplefilter </filter-class> defines the Class Name of the filter;
<Init-param>... </init-param> defines the initialization parameters of the filter. These parameters can be obtained in the getinitparameter method of filterconfig.
Among the above tags, only <filter-Name>... </filter-Name> and <filter-mapping>... </filter-mapping> is required, and others are optional.

<Filter-mapping>... </filter-mapping> indicates the relationship between a filter and a Web Resource (servlet, JSP:
<Filter-Name>... </filter-Name> specifies the name of the filter.
<URL-pattern>... </url-pattern> sets the URL style filtered by the filter.
<Servlet-Name>... </servlet-Name> directly sets the name of the Servet filtered by the filter (of course, this servlet must be defined in Web. XML ).

 

 

 

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.