filter类里写代码
包装类思想
1 package cn.itcast.filter; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletRequestWrapper; 14 import javax.servlet.http.HttpServletResponse; 15 16 public class AllCharacterEncodingFilter implements Filter { 17 private FilterConfig filterConfig; 18 19 public void init(FilterConfig filterConfig) throws ServletException { 20 this.filterConfig = filterConfig; 21 } 22 public void doFilter(ServletRequest req, ServletResponse resp, 23 FilterChain chain) throws IOException, ServletException { 24 HttpServletRequest request = (HttpServletRequest)req; 25 HttpServletResponse response = (HttpServletResponse)resp; 26 //获取配置的过滤器参数encoding的值:如果没有,给定一个默认值 27 String encoding = filterConfig.getInitParameter("encoding"); 28 if(encoding==null) 29 encoding = "UTF-8"; 30 //设置POST请求方式的中文请求参数的编码 31 request.setCharacterEncoding(encoding); 32 //设置响应输出时的编码:字符流和字节流 33 response.setCharacterEncoding(encoding);//字节流输出时通知客户端的解码码表 34 response.setContentType("text/html;charset="+encoding);//字节流输出时通知客户端的解码码表;字符流:字符流输出时使用的码表及通知客户端解码用的码表 35 36 MyHttpServletRequest mrequest = new MyHttpServletRequest(request); 37 //放行 38 chain.doFilter(mrequest, response); 39 } 40 41 42 public void destroy() { 43 44 } 45 } 46 47 //HttpServletRequestWrapper内部的代码与连接池原理中那个默认适配器一样 48 class MyHttpServletRequest extends HttpServletRequestWrapper{ 49 public MyHttpServletRequest(HttpServletRequest request){ 50 super(request); 51 } 52 //只对get请求方式进行改写 53 public String getParameter(String name) { 54 String value = super.getParameter(name); 55 if(value==null) 56 return value; 57 //得到请求方式 58 String method = super.getMethod(); 59 if("get".equalsIgnoreCase(method)){ 60 try { 61 value = new String(value.getBytes("ISO-8859-1"),super.getCharacterEncoding()); 62 } catch (UnsupportedEncodingException e) { 63 e.printStackTrace(); 64 } 65 } 66 return value; 67 } 68 69 } 70 class MyHttpServletRequest1 extends HttpServletRequestWrapper{ 71 private HttpServletRequest request; 72 public MyHttpServletRequest1(HttpServletRequest request){ 73 super(request); 74 this.request = request; 75 } 76 //只对get请求方式进行改写 77 public String getParameter(String name) { 78 String value = request.getParameter(name); 79 if(value==null) 80 return value; 81 //得到请求方式 82 String method = request.getMethod(); 83 if("get".equalsIgnoreCase(method)){ 84 try { 85 value = new String(value.getBytes("ISO-8859-1"),"UTF-8"); 86 } catch (UnsupportedEncodingException e) { 87 e.printStackTrace(); 88 } 89 } 90 return value; 91 } 92 93 }
动态代理类思想
1 package cn.itcast.web.bbs.mvc.filter; 2 3 import java.io.IOException; 4 import java.lang.reflect.InvocationHandler; 5 import java.lang.reflect.Method; 6 import java.lang.reflect.Proxy; 7 8 import javax.servlet.Filter; 9 import javax.servlet.FilterChain; 10 import javax.servlet.FilterConfig; 11 import javax.servlet.ServletException; 12 import javax.servlet.ServletRequest; 13 import javax.servlet.ServletResponse; 14 import javax.servlet.annotation.WebFilter; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 18 public class EncodingFilter implements Filter { 19 20 public EncodingFilter() { 21 } 22 23 public void destroy() { 24 } 25 26 /** 27 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 28 */ 29 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 30 31 HttpServletRequest req=(HttpServletRequest) request; 32 HttpServletResponse res=(HttpServletResponse) response; 33 RequestProxy requestProxy=new RequestProxy(req); 34 HttpServletRequest xx=requestProxy.getRequestProxy(); 35 chain.doFilter(xx, response); 36 } 37 38 public void init(FilterConfig fConfig) throws ServletException { 39 } 40 } 41 class RequestProxy 42 { 43 private HttpServletRequest request; 44 public RequestProxy(HttpServletRequest request) { 45 super(); 46 this.request = request; 47 } 48 //产生动态代理对象 49 public HttpServletRequest getRequestProxy() 50 { 51 return (HttpServletRequest) Proxy.newProxyInstance(RequestProxy.class 52 .getClassLoader(), request.getClass().getInterfaces(), 53 new InvocationHandler() { 54 @Override 55 public Object invoke(Object proxy, Method method, 56 Object[] args) throws Throwable { 57 Object returnValue=""; 58 String methodName=method.getName(); 59 if(methodName.equals("getParameter")) 60 { 61 String methodType=request.getMethod(); 62 if("GET".equalsIgnoreCase((methodType))) 63 {//乱码 64 String temp = (String) method.invoke(request,args); 65 //还原 66 byte[] buf = temp.getBytes("ISO8859-1"); 67 //正码 68 returnValue = new String(buf,"UTF-8"); 69 } 70 else if("POST".equalsIgnoreCase(methodType)) 71 { 72 request.setCharacterEncoding("UTF-8"); 73 returnValue = method.invoke(request,args); 74 } 75 } 76 else 77 { 78 returnValue=method.invoke(request, args); 79 } 80 return returnValue; 81 } 82 }); 83 } 84 85 }
原文:http://www.cnblogs.com/friends-wf/p/3753830.html