新建一个Filter,配置设置为REQUEST或全局
代码如下:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest)request;
HttpServletRequest myrequest = (HttpServletRequest) Proxy.newProxyInstance(EcodeingFilter.class.getClassLoader(),
req.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object obj = null;
if ("getParameter".equalsIgnoreCase(method.getName())) {
String md = req.getMethod();
if ("GET".equalsIgnoreCase(md)) {
String v = (String) method.invoke(req, args);
return new String(v.getBytes("iso-8859-1"), "UTF-8");
}else if ("POST".equals(md)) {
req.setCharacterEncoding("UTF-8");
return method.invoke(req, args);
}
}else {//PUT
return method.invoke(req, args);
}
return obj;
}
} );
chain.doFilter(myrequest, response);
}