使用动态代理解决get参数中文乱码
public class RequestHandler implements InvocationHandler {HttpServletRequest request;public RequestHandler(HttpServletRequest request){this.request=request;}public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {// TODO Auto-generated method stub Object result=method.invoke(request, args); if(method.getName().equals("getParameter")){ //根据项目的统一编码类型调整红色部分 if(result != null){ result =new String(((String)result).getBytes("ISO-8859-1"),"UTF-8"); } }return result;}}
?在web.xml配置一个过滤器,关键代码如下
?
public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {InvocationHandler handler = new RequestHandler((HttpServletRequest)arg0);HttpServletRequest request=(HttpServletRequest)Proxy.newProxyInstance(arg0.getClass().getClassLoader(), arg0.getClass().getInterfaces(), handler);arg2.doFilter(request, arg1);}
?通过以上程序的拦截处理,你在action中使用request.getParameter方法获取参数时就可以得到正确的解码值
?