05 "filter to solve the Chinese scrambling of the request (inherited from HttpServletRequestWrapper)

1) create a new EncodingFilter

Create a new filter for all requests in the past. The decoding method used to process the request parameters is UTF-8, which solves the Chinese scrambling

/**
 * Unified coding
 * @author Administrator
 */
@WebFilter("/*")//Filter all paths
public class EncodingFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		//1.Strong turn
		HttpServletRequest request=(HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse) resp;
		
		//2.Release
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}

2) custom request inherits HttpServletRequestWrapper

HttpServletRequestWrapper implements the HttpServletRequest interface, writes classes to inherit HttpServletRequestWrapper, and then implements three methods: String getParameter(String name), String[] getParameterValues(String name), String[] getParameterValues(String name). They can be changed according to our needs, such as setting characters and removing spaces.
Create the class MyRequest in the EncodingFilter file:

class MyRequest extends HttpServletRequestWrapper{
	private HttpServletRequest request;
	private boolean flag=true;
	
	
	public MyRequest(HttpServletRequest request) {
		super(request);
		this.request=request;
	}
	
	@Override
	public String getParameter(String name) {  
		if(name==null || name.trim().length()==0){
			return null;
		}
		String[] values = getParameterValues(name);
		if(values==null || values.length==0){
			return null;
		}
		
		return values[0];
	}
	
	@Override
	/**
	 * hobby=[eat,drink]
	 */
	public String[] getParameterValues(String name) {
		if(name==null || name.trim().length()==0){
			return null;
		}
		Map<String, String[]> map = getParameterMap();
		if(map==null || map.size()==0){
			return null;
		}
		
		return map.get(name);
	}
	
	@Override
	/**
	 * map{ username=[tom],password=[123],hobby=[eat,drink]}
	 */
	public Map<String,String[]> getParameterMap() {  
		
		/**
		 * First determine the request mode
		 * If post request. Setchar... (UTF-8)
		 * If the value in the map is traversed and encoded for get, it is OK
		 */
		String method = request.getMethod();
		if("post".equalsIgnoreCase(method)){
			try {
				request.setCharacterEncoding("utf-8");
				return request.getParameterMap();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("get".equalsIgnoreCase(method)){
			Map<String,String[]> map = request.getParameterMap();
			//You need to traverse the map to modify the encoding of each data of value
			if(flag){
				for (String key:map.keySet()) {
					String[] arr = map.get(key);
					//Continue traversing array
					for(int i=0;i<arr.length;i++){
						//Code
						try {
							arr[i]=new String(arr[i].getBytes("iso8859-1"),"utf-8");
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
					}
				}
				flag=false;
			}
			return map;
		}
		
		return super.getParameterMap();
	}
	
}

3) use custom request to complete the coding setting in the filter

	...
	//2. release
	chain.doFilter(new MyRequest(request), response);
	...

In this way, the getParameters method of the wrapper is invoked in servlet to get the parameters, and the conversion process of the character encoding has been completed.

4) test registration

View database:

Tags: encoding Database

Posted on Mon, 02 Dec 2019 16:56:25 -0500 by getmizanur