美文网首页
动态代理模式(w10_2)匿名内部类形式

动态代理模式(w10_2)匿名内部类形式

作者: 康明 | 来源:发表于2017-01-04 21:33 被阅读0次
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            // TODO Auto-generated method stub
            // place your code here
    
            //处理poset提交方法,参数的中文乱码
            request.setCharacterEncoding("utf8");
            //处理响应内容的中文乱码
            response.setContentType("text/html;charset=utf8");
            
            //动态代理
            //创建一个HttpServletRequest的对象的代理对象
            
            //第一个参数:代理的对象的类加载器
            //第二个参数,代理的对象的Class类型
            //第三个参数,InvocationHandler的对象,重写invoke方法,实现对代理的对象的某些方法的功能上的扩展(执行被代理的对象的 接口方法)
            HttpServletRequest proxyRequset = (HttpServletRequest)Proxy.newProxyInstance(
                    HttpServletRequest.class.getClassLoader(), 
                    new Class[]{HttpServletRequest.class}, 
                    new InvocationHandler() {
                        
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            // TODO Auto-generated method stub
                            
                            //获取方法名
                            String mName = method.getName();
                        
                            //对getParameter方法进行功能扩展
                            if(mName.equals("getParameter")){
                                String v = request.getParameter((String)args[0]);
                                if(v == null || v.equals("")){
                                    return null;
                                }
                                //获取表单提交方法
                                String m = ((HttpServletRequest)request).getMethod();
                                //处理get提交方式的中文乱码
                                if(m.equalsIgnoreCase("get")){
                                    v = new  String(v.getBytes("iso-8859-1"), "utf8");
                                }
                                
                                //处理敏感词
                                for(String str : dirtyList){
                                    if(v.contains(str)){
                                        v = v.replace(str, "***");
                                    }
                                }
                                
                                return v;
                            }else{
                                //其他不需要特殊处理的方法,直接调用
                                return method.invoke(request, args);
                            }
                            
                        }
                    });
            
            // pass the request along the filter chain
            //将httpservletrequest的代理对象传递给要访问的资源
            chain.doFilter(proxyRequset, response);
        }
    
    

    相关文章

      网友评论

          本文标题:动态代理模式(w10_2)匿名内部类形式

          本文链接:https://www.haomeiwen.com/subject/jujavttx.html