filter

1. Filter Filter

1. Introduction to filter

Filter is the filtering of the client's access to resources. It is released when the conditions are met, but not when the conditions are not met, and logical processing can be performed before and after the target resource is accessed.

2. Quick start

step:

1) Write a filter class that implements the Filter interface

2) Implement methods that have not yet been implemented in the interface (focus on implementing the doFilter method)

3) Configure in web.xml (mainly configure which resources to filter)

 

3. Detailed API of Filter

(1) The filter life cycle and its methods related to the life cycle

The Filter interface has three methods, and these three are methods related to the life of Filter

init(Filterconfig): On behalf of the filter object initialization method, it is executed when the filter object is created

doFilter(ServletRequest,ServletResponse,FilterCha): The core method of performing filtering on behalf of the filter. If a resource has been configured to be filtered by this filter, the doFilter method will be executed every time the resource is accessed.

Destory(): Represents the filter destruction method. This method is executed when the filter object is destroyed.

 

The life cycle of the Filter object:

When filter is created: the filter object is created when the server starts

When is the filter destroyed: the filter is destroyed when the server shuts down

 

(2) Detailed AP of Filter

1)init(FilterConfig)

The parameter config represents the object of the configuration information of the Filter object, and the internal encapsulation is the configuration information of the filter.

 

2) destroy() method

Executed when the filter object is destroyed

3) doFilter method

doFilter(ServletRequest,ServletResponse,FilterChain)

Among the parameters:

ServletRequest/ServletResponse: Each time the doFilter method is executed, the web container is responsible for creating a request and a response object and passing it in as the parameters of doFilter. The request and the response are the request and response when accessing the service method of the target resource.

FilterChain: The filter chain object, the request can be released through the doFilter method of the object

Pay attention to the execution order of filters

 


4. Filter configuration

 

When url-pattern is configured

1) Exactly match /sertvle1

2) Directory matches /aaa/bbb/* ---- the most

/user/*: access to foreground resources to enter this filter

/admin/*: Execute this filter when accessing resources in the background

3) The extension matches *.abc *.jsp

 

Note: url-pattern can be replaced by servlet-name or mixed

 

 

dispatcher: the way to access (understand)

REQUEST: The default value, which means that the filter is executed when a resource is directly accessed

FORWARD: filter is only executed when forwarding

INCLUDE: Execute filter when resources are included

ERROR: jumping when an error occurs is to execute filter

 

 

 

Summarize the role of Filter?

1) Extraction of public code

2) The methods in request and response can be enhanced (decorator pattern/dynamic proxy)

3) Perform permission control

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024969&siteId=291194637