码迷,mamicode.com
首页 > 其他好文 > 详细

解决全站字符编码问题--动态代理和静态代理

时间:2018-03-06 16:58:55      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:代理模式

动态代理和静态代理的区别
动态代理是在运行时,将代理类加载到内存中
静态代理是提前把代理类写好,然后再启动项目的时候把代理类加载到内存中

动态代理

public class EncodingFilter implements Filter {

public void destroy() {
}

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
        throws IOException, ServletException {
    //0 强转
    final HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;

    //1 post乱码
    request.setCharacterEncoding("UTF-8");

    //2 使用动态代理增强request
    HttpServletRequest proxyRequest = (HttpServletRequest)Proxy.newProxyInstance(
                            EncodingFilter.class.getClassLoader(), 
                            new Class[] { HttpServletRequest.class } , 
                            new InvocationHandler(){

                                @Override
                                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                                    //增强
                                    String methodName = method.getName();
                                    if("getParameter".equals(methodName)){
                                        //args 所有参数 --> getParameter("username");
                                        //1 从tomcat request获得原始数据(乱码)
                                        String value = request.getParameter((String)args[0]);
                                        //2 如果get请求处理乱码
                                        if("GET".equals(request.getMethod())){
                                            value = new String( value.getBytes("ISO-8859-1") , "UTF-8");
                                        }
                                        //3 将结果返回
                                        return value;
                                    }

                                    //不需要增强的直接执行目标类的方法
                                    return method.invoke(request, args);
                                }

                            });

    chain.doFilter(proxyRequest, response);

}

public void init(FilterConfig fConfig) throws ServletException {
}

}

静态代理(装饰者模式)

public class EncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
        throws IOException, ServletException {
    //0 强转
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;

    //1 post乱码
    request.setCharacterEncoding("UTF-8");

    //2 使用装饰者增强request
    MyRequest myRequest = new MyRequest(request);

    chain.doFilter(myRequest, response);

}

public void init(FilterConfig fConfig) throws ServletException {
}

}

/**

  • 设计模式:固定的代码,用于解决固定问题
    • 装饰者:对方法进行增强
      1.确定接口
      2.提供成员变量
      3.构造方法
      4.增强需要的方法
      *5.其他方法默认调用tomcat对应方法即可
  • sun 提供 HttpServletRequest 接口使用装饰者编写默认类,及所有的方法都没有增强的。
    • 之后我们只需要继承即可
  • 增强response对象,提供使用装饰者设计模式编写默认类:HttpServletResponseWrapper
    */

    public class MyRequest extends HttpServletRequestWrapper {
    private HttpServletRequest request; //tomcat request

    public MyRequest(HttpServletRequest request){
        super(request);
        this.request = request;
    }

    //增强

    @Override
    public String getParameter(String name) {

    //1 直接从tomcat的request获得数据
    String value = this.request.getParameter(name);
    
    //2 如果是get,处理
    // * 请求方式
    String method = this.request.getMethod();
    if("GET".equals(method)){
        try {
            value = new String( value.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    
        return value;
    }
    
    }

解决全站字符编码问题--动态代理和静态代理

标签:代理模式

原文地址:http://blog.51cto.com/13579086/2083455

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!