1、配置一个过滤器,拦截所有请求
package cn.demo.filter;import java.io.IOException;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.HttpServletResponse;/** * 全栈乱码过滤器 * */public class MyFilter implements Filter { private String value; private String type; @Override public void init(FilterConfig filterConfig) throws ServletException { value = filterConfig.getInitParameter("encoding"); type = filterConfig.getInitParameter("type"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 强转成子接口类型,使用更多api HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServletResponse = (HttpServletResponse) response; MyRequest myRequest = new MyRequest(httpServletRequest);// 构造方法注入,MyResquest中重写三个方法,解决乱码 httpServletResponse.setContentType(type + ";charSet=" + value); chain.doFilter(myRequest, httpServletResponse); } @Override public void destroy() { }}web.xml配置MyFilter cn.demo.filter.MyFilter encoding utf-8 type text/html MyFilter /*
2、MyRequest类重写三个方法
package cn.demo.filter;import java.io.UnsupportedEncodingException;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;public class MyRequest extends HttpServletRequestWrapper { private HttpServletRequest request; private boolean flag = false; public MyRequest(HttpServletRequest request) { super(request); this.request = request; } // 重写三个方法 @Override public Map getParameterMap() { 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(); } } // 遍历取出每个元素,先编码,再解码 if ("get".equalsIgnoreCase(method)) { if (!flag) { Mapmap = request.getParameterMap(); if (map != null && map.size() != 0) { // 遍历 for (String key : map.keySet()) { String[] values = map.get(key); if (values != null && values.length != 0) { for (int i = 0; i < values.length; i++) { try { // 每一个元素先编码再解码 values[i] = new String(values[i].getBytes("iso-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } flag = true; return map; } } return super.getParameterMap(); } @Override public String[] getParameterValues(String name) { Map map = getParameterMap(); if (map != null && map.size() != 0) { String[] values = map.get(name); if (values != null && values.length != 0) { return values; } } return super.getParameterValues(name); } @Override public String getParameter(String name) { Map map = getParameterMap(); if (map != null && map.size() != 0) { String[] values = map.get(name); if (values != null && values.length != 0) { return values[0]; } } return super.getParameter(name); }}