Annotation of Filter usage and Annotation of Filter usage

Source: Internet
Author: User

Annotation of Filter usage and Annotation of Filter usage

I. Concepts and functions of filter

A Filter is also called a Filter.FilterIs a newly added function after Servlet2.3. It can be changed to a request or a response. It can pre-process the request before the request arrives at the Servlet. It can also process the response when the response leaves the Servlet. In short, it can be called the Servlet chain. The general function is as follows:

1. Before the request arrives at the Servlet:

1) unified authentication of user requests;

2) encoding conversion;

3) Replace the data sent by the user, or encrypt the data.

2. After response leaves Servlet:

1) modify the response header and data.

Ii. Code Implementation

First, implement the javax. servlet. Filter interface.And three methods:

1. init (FilterConfig filterConfig) --> the initialization filter is used to obtain initialization parameters.

2. doFilter (ServletRequestServletRequest, ServletResponseServletResponse, FilterChainChain) --> All operations related to filter processing are completed here. You need to call chain. doFilter (ServletRequest,ServletResponse) Method to pass the request to the backend. It can be a call to the doFilter method of the next filter or a corresponding web component. Therefore, the Filter adopts the responsibility chain design mode.

3. The destroy () filter is called before the Servlet container destroys the filter instance to release the occupied resources.

Final encoding implementation:

Uniform authentication of user requests to obtain the user name. If the user name is blank, it is transferred to the logon interface; otherwise, it is allowed.

@ WebFilter:

Attribute name type description

FilterName String specifies the name attribute of the filter, which is equivalent to <filter-name>

Value String [] This attribute is equivalent to the urlPatterns attribute.

UrlPatterns String [] specifies the URL matching mode of the filter, which is equivalent to <url-pattern>

ServletNames String [] specifies the Servlet to which the filter applies. The value is the name attribute of @ WebServlet or the value of <servlet-name>.

DispatcherTypes DispatcherType specifies the forwarding mode of the filter. The values include async error forward include request.

InitParams WebInitParam [] specifies a set of filter initialization parameters, which are equivalent to <init-Param>

AsyncSupported boolean specifies whether the filter supports asynchronous operation mode, which is equivalent to <async-supported>

Description String: The description of the filter, which is equivalent to <description>

DisplayName String the display name of the filter, which is usually used together with the tool and is equivalent to <display-name>

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.