Filter filter way to solve the garbage problem submitted by post and get

post and get

post: a request is sent, jsp content sent encoding jsp pages provided.
get: url address is sent, the encoding jsp page setup no effect, it can only be a web container default encoding encoding.

post garbage problem

When the request is sent, the encoding format is provided.

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf");
		//设置响应的编码格式必须编写在标准输出流的前面,后面不起作用
		response.setContentType("text/html;charset=utf-8");
		chain.doFilter(request, response);

	}

get the garbage problem

When we can not change the encoding format web container, it can be decorated on request request, to achieve the results we want. Here it must be understood that the decorator design pattern (without modifying the original content of the decorators, only to be added or enhanced single function decorators).
request request is the request parameters into a map, the time to send a request to get way, put the value of the map is garbled, we can map the original value encoding, decoding and then rewritten into a new map in . We can solve the garbage problem.

1. The base class
request in a base class HttpServletRequestWrapper default implementation.
2. decorator (inherited base class implements special features)

package com.java.decorator;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class CustomRequest extends HttpServletRequestWrapper{
	//获取基类对象
	public CustomRequest(HttpServletRequest request) {
		super(request);
	}
	@Override
	public Map<String, String[]> getParameterMap() {
		// TODO Auto-generated method stub
		//创建新的map
		Map<String, String[]> newMap = new HashMap<String, String[]>();
		//获取原先的map
		Map<String, String[]> oldMap = super.getParameterMap();
		
		try {
		//遍历原先的map
			for(String key:oldMap.keySet()){
				String values[] = oldMap.get(key);
				for(int i=0;i<values.length;i++){
				//对原先的map进行编码和解码
					byte[] bytes = values[i].getBytes("utf-8");
					values[i] = new String(bytes,"utf-8");
				}
				//放到新的map
				newMap.put(key, values);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//返回新的map
		return newMap;
	}
	@Override
	public Enumeration<String> getParameterNames() {
		Map<String, String[]> map = this.getParameterMap();
		Set<String> keySet = map.keySet();
		//keySet中不能返回Enumeration,强转为Vector 
		Vector ketVector = (Vector) keySet;
		return ketVector.elements();
	}
	@Override
	public String getParameter(String name) {
		// TODO Auto-generated method stub
		
		return  this.getParameterValues(name)[0];
	}
	@Override
	public String[] getParameterValues(String name) {
		// TODO Auto-generated method stub
		Map<String, String[]> map = this.getParameterMap();
		return map.get(name);
	}
	
}

3. The request for decoration

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		//传递被装饰者对象获取增强后的对象
		request = new CustomRequest((HttpServletRequest) request);
		response.setContentType("text/html;charset=utf-8");
		chain.doFilter(request, response);

	}
Published 114 original articles · won praise 8 · views 5473

Guess you like

Origin blog.csdn.net/OVO_LQ_Start/article/details/105012882