(一)通过代理解决全站乱码 //通过代理解决全站乱码问题 class CharacterEncodingFilter2 implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { final HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; request.setCharacterEncoding("UTF-8"); //post get request.getParameter() //servlet----> requestProxy.getCookie requestProxy.get chain.doFilter((ServletRequest) Proxy.newProxyInstance(CharacterEncodingFilter2.class.getClassLoader(), request.getClass().getInterfaces(), new InvocationHandler(){ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(!method.getName().equals("getParameter")){ return method.invoke(request, args); } if(!request.getMethod().equalsIgnoreCase("get")){ return method.invoke(request, args); } String value = (String) method.invoke(request, args); if(value==null){ return null; } return new String(value.getBytes("iso8859-1"),"UTF-8"); } }), response); } public void destroy() { } public void init(FilterConfig filterConfig) throws ServletException { } }
|