介绍二种写法(写法其实类似):
写法一:
/** * cn.jbit.struts2.filter.CharFilter * 编码过滤器A * 2014-4-21 * gyy */ package cn.jbit.struts2.filter; import java.io.IOException; import java.util.Map; import java.util.Set; 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 CharFilter implements Filter { private FilterConfig filterConfig = null; private String encoding; // 销毁 public void destroy() { this.filterConfig = null; } @SuppressWarnings("unchecked") public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse resp = (HttpServletResponse) response; request.setCharacterEncoding(encoding); resp.setContentType("text/html;charset=" + encoding); System.out.println(request.getQueryString()); // 处理GET参数 if (request.getMethod().equalsIgnoreCase("get")) { Map paramMap = req.getParameterMap(); System.out.println("paramMap.size=" + paramMap.size()); System.out.println(((String[]) paramMap.get("id"))[0]); System.out.println(((String[]) paramMap.get("name"))[0]); String[] queryStringArray = { "" }; if (request.getQueryString() != null) { queryStringArray = request.getQueryString().split("&"); } // 获取参数名 $1为获取"="前(.*)部分 for (int i = 0; i < queryStringArray.length; i++) { queryStringArray[i] = queryStringArray[i].replaceAll( "(.*)=(.*)", "$1"); } System.out.println("-----------------------------------"); for (String str : queryStringArray) { System.out.println(str); } System.out.println("-----------------------------------"); Set<String> keySet = paramMap.keySet(); for (String key : keySet) { System.out.println("key=" + key); boolean isFromGet = false; for (String paramFromGet : queryStringArray) { if (key.equals(paramFromGet)) { isFromGet = true; } } if (!isFromGet) { continue; } String[] paramArray = (String[]) paramMap.get(key); // 设置编码方式 for (int i = 0; i < paramArray.length; i++) { paramArray[i] = new String(paramArray[i] .getBytes("ISO8859-1"), encoding); } } } chain.doFilter(req, resp); } // 初始化 public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; // 获取编码方式 this.encoding = this.filterConfig.getInitParameter("encoding"); } }
写法二:
/** * cn.jbit.struts2.filter.EncodingFilter * 编码过滤器B * 2014-4-21 * gyy */ package cn.jbit.struts2.filter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; import java.util.Set; 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 { private FilterConfig filterConfig; private String encoding; class MyRequest extends HttpServletRequestWrapper { // 请求包装类 public MyRequest(HttpServletRequest request) { super(request); } // 编码转换 protected String encodingConvert(String str) { if (null == str) { return null; } else { try { byte[] bytes = str.getBytes("ISO8859-1"); return new String(bytes, encoding); } catch (UnsupportedEncodingException e) { } } return null; } @Override public String getParameter(String name) { return encodingConvert(name); } @Override public String[] getParameterValues(String name) { String[] values = super.getParameterValues(name); if (null != values) { for (int i = 0; i < values.length; i++) { values[i] = encodingConvert(values[i]); } } return values; } } // 销毁 public void destroy() { this.filterConfig = null; this.encoding = null; } @SuppressWarnings("unchecked") public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if ("GET".equals(httpRequest.getMethod())) { request = new MyRequest(httpRequest); // 获取请求参数Map Map paramMap = request.getParameterMap(); Set<String> keySet = paramMap.keySet(); for (String key : keySet) { String[] param = (String[]) paramMap.get(key); // System.out.println(paramArray.length); // 设置编码方式 if (param.length == 1) {// 单值 param[0] = request.getParameter(param[0]); } else {// 多值 param = request.getParameterValues(key); } } } chain.doFilter(request, response); } // 初始化 public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = this.filterConfig.getInitParameter("encoding"); } }