思路:使用装饰类包装request请求
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class EncodeFilter implements Filter {
private FilterConfig config = null;
private ServletContext context = null;
private String encode = null;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//响应乱码解决
response.setCharacterEncoding(encode);
response.setContentType("text/html;charset=utf-8");
chain.doFilter(new MyHttpServletRequest((HttpServletRequest)request), response); //利用装饰设计模式,改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题
}
public void init(FilterConfig filterConfig) throws ServletException {
this.config = filterConfig;
this.context = filterConfig.getServletContext();
this.encode = context.getInitParameter("encode");
}
/**
* 装饰者模式
* @author windows
*
*/
class MyHttpServletRequest extends HttpServletRequestWrapper{
//被装饰者
private HttpServletRequest request = null;
private boolean isNotEncode = true;
public MyHttpServletRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
@Override
public String getParameter(String name) {
return getParameterValues(name)==null?null:getParameterValues(name)[0];
}
@Override
public Map<String, String[]> getParameterMap() {
try {
if(request.getMethod().equalsIgnoreCase("POST")){
request.setCharacterEncoding(encode);
return request.getParameterMap();
}else if(request.getMethod().equalsIgnoreCase("GET")){
Map<String, String[]> map = request.getParameterMap();
if (isNotEncode) {
for (Map.Entry<String, String[]> entry : map.entrySet()) {
String[] vs = entry.getValue();
for (int i = 0; i < vs.length; i++) {
vs[i] = new String(vs[i].getBytes("iso-8859-1"),encode);
}
}
isNotEncode = false;
}
return map;
}else{
return request.getParameterMap();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
public String[] getParameterValues(String name) {
// TODO Auto-generated method stub
return getParameterMap().get(name);
}
}
}