标签:eth Servle cer main system targe target vax 处理
//---------------TargetInterface package cn.ms.proxy; public interface TargetInterface { public void method1(); public String method2(); public int method3(int x); } //---------------Target package cn.ms.proxy; public class Target implements TargetInterface { @Override public void method1() { System.out.println("method1 running..."); } @Override public String method2() { System.out.println("method2 running..."); return "method2"; } @Override public int method3(int x) { System.out.println("method3 running..."); return x; } } //---------------ProxyTest package cn.ms.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyTest { public static void main(String[] args) { Target target = new Target(); TargetInterface newProxy = (TargetInterface) Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("目标方法前逻辑"); Object invoke = method.invoke(target, args); System.out.println("目标方法后逻辑"); return invoke; } }); newProxy.method1(); System.out.println("------------------ method1 end ------------------"); String returnMethod2 = newProxy.method2(); System.out.println(returnMethod2); System.out.println("------------------ method2 end ------------------"); int returnMethod3 = newProxy.method3(7); System.out.println(returnMethod3); System.out.println("------------------ method3 end ------------------"); } }
package com.ithiema.web.filter; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; 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; public class EncodingProxyFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletRequest enhanceRequest = (HttpServletRequest) Proxy.newProxyInstance( httpServletRequest.getClass().getClassLoader(), httpServletRequest.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(method.getName().equals("getParameter")){ String invoke = (String) method.invoke(httpServletRequest, args); invoke = new String(invoke.getBytes("iso-8859-1"), "UTF-8"); return invoke; } return method.invoke(httpServletRequest, args); } }); chain.doFilter(enhanceRequest, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
标签:eth Servle cer main system targe target vax 处理
原文地址:https://www.cnblogs.com/ms-grf/p/9029135.html