Filter notes (mainly see the ppt of the dark horse tutorial)

1 Introduction

Filter is to filter the client's access to resources. If the conditions are met, it will pass, otherwise it will not pass. Filter the requested things

2. Use steps

1) Write a filter class to implement the Filter interface
2) Implement methods that have not yet been implemented in the interface (emphasize the implementation of the doFilter method)
3) Configure in web.xml (mainly configure which resources to filter)

3. Build the filter class

package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterConfig;

//直接加满不多说
public class OneFilter implements Filter {
    
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException{
    
    
    	
    }
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
    
    
		// TODO Auto-generated method stub
		
	}
    @Override
    public void destroy(){
    
    	
    }
}

Function meaning:
init(Filterconfig): on behalf of the filter object initialization method
doFilter (ServletRequest, ServletResponse, FilterCha): on behalf of the core method of filter execution when the filter object is created , if a resource has been configured to this filter for filtering, then The doFilter method
destroy() is executed every time this resource is accessed : it means the filter destruction method is executed when the filter object is destroyed. The
internal API explanation of the function:
1) init (FilterConfig)
where the parameter config represents the object of the configuration information of the Filter object, The internal encapsulation is the configuration information of the filter.
2) destroy()
is executed when the filter object is destroyed
3) doFilter method
doFilter(ServletRequest, ServletResponse, FilterChain)
The parameters:
ServletRequest/ServletResponse: each time the doFilter method is executed, the web container is responsible for creating a request and a response object as the doFilter The parameters are passed in. The request and the response are the request and response when accessing the service method of the target resource.
FilterChain: Filter chain object, through the doFilter method of this object, the request can be released.

Generally, the filter is a chain, and there are several in web.xml. Connect one by one

4. Distribution web.xml

Insert picture description here
When url-pattern is configured
1) Exactly match /sertvle1
2) Directory matches /aaa/bbb/* ----The most
/user/ : access to the front-end resources enter this filter
/admin/
: execute this when accessing the back-end resources Filter
3) The extension matches *.abc *.jsp

Note: url-pattern can be replaced by servlet-name, or can be mixed with
dispatcher: access method (understand)
REQUEST: default value, which means to execute filter when accessing a resource directly.
FORWARD: execute only when forwarding filter
INCLUDE: execute when resources are included filter
ERROR: to jump when an error occurs is to execute filter

Summary:
1) Extraction of public code
2) The methods in request and response can be enhanced (decorator mode/dynamic agent)
3) Authority control

Guess you like

Origin blog.csdn.net/weixin_45743162/article/details/109825154