javaweb -- Filter

Keywords: Java xml JSP

Filter

1. Introduction to filter
Filter is a filter for client access resources, which can be released in accordance with the conditions and not in conformity with the conditions. It can also be logically processed before and after access to the target resources.
2. Quick Start
Steps:
1) Write a Filter class to implement the Filter interface
2) Implementing methods that are not yet implemented in the interface (focusing on the doFilter method)
3) configure in web.xml (mainly which resources should be filtered for configuration)

  • EncodingFilter class
package com.ithiema.web.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class EncodingFilter implements Filter{

	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		//request.setCharacterEncoding("UTF-8");
		
		//Enhance the getParameter method of the request before passing it
		/*
		 * Decorator Mode (Packaging)
		 * 
		 * 1,Enhanced Classes and Enhanced Classes Realize Unified Interfaces
		 * 2,Enhanced classes are introduced into enhanced classes
		 * 3,Method rewriting that requires enhancement. Method calls that do not require enhancement are enhanced
		 * 
		 */
		
		//Enhanced objects
		HttpServletRequest req = (HttpServletRequest) request;
		//Enhanced object
		EnhanceRequest enhanceRequest = new EnhanceRequest(req);
		
		
		chain.doFilter(enhanceRequest, response);
		
	}

	@Override
	public void destroy() {
		
	}
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

}

class EnhanceRequest extends HttpServletRequestWrapper{
	
	private HttpServletRequest request;

	public EnhanceRequest(HttpServletRequest request) {
		super(request);
		this.request = request;
	}
	
	//Enhancement of getParaameter
	@Override
	public String getParameter(String name) {
		String parameter = request.getParameter(name);//Random code
		try {
			parameter = new String(parameter.getBytes("iso8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return parameter;
	}
	
}

3. Detailed API of Filter
(1)filter life cycle and its related methods
The Filter interface has three methods, and these three methods are all related to the life of the Filter.

1.init(Filterconfig): Executed on behalf of the filter object initialization method filter object creation

2.doFilter(ServletRequest,ServletResponse,FilterCha): The core method of performing filtering on behalf of a filter. If a resource is already configured to filter on this filter, the doFilter method is executed every time the resource is accessed.
3.destory(): Represents a filter destruction method that is executed when the filter object is destroyed

The lifecycle of Filter objects:
When Filter is created: The filter object is created at server startup
When Filter Destroys: Filter Destroys When Server Closes

(2) AP Explanation of Filter
1)init(FilterConfig)
The parameter config represents the object of configuration information of the filter object, and the internal encapsulation is the configuration information of the filter.

2) destory() method
Execution when the filter object is destroyed
3) doFilter method
doFilter(ServletRequest,ServletResponse,FilterChain)
The parameters are as follows:
ServletRequest/ServletResponse: Each time the doFilter method is executed, the web container is responsible for creating a request and a response object to be passed in as parameters of the doFilter. The request is the request and response when accessing the service method of the target resource.
FilterChain: A filter chain object through which the request can be released by the doFilter method

4. Configuration of Filter

url-pattern configuration
1) Perfect matching/sertvle1
2) directory matching / aaa/bbb /* - most
/ user/: Access to the front-end resources into this filter
/ admin/: Execute this filter when accessing resources in the background
3) Extension matching *. ABC *. JSP

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

dispatcher: How to access (understand)
REQUEST: Default value to execute filter when accessing a resource directly
FORWARD: filter is executed only when forwarding
INCLUDE: Execute filter when containing resources
ERROR: To jump when an error occurs is to execute a filter

Summarize the role of Filter?
1) Extraction of Common Code
2) Methods in request and response can be enhanced (Decorator Mode/Dynamic Agent)
3) Access control

Posted by shergar1983 on Sun, 15 Sep 2019 00:17:59 -0700