zoukankan      html  css  js  c++  java
  • Listener 监听对象的属性变化

      Listener用于监听Session、context、Request的属性变化,接口名称格式为xxxAttributeListener,包括HttpSessionAttributeListener、ServletContextAttributeLIstener、ServletRequesAttributeListener。当向被监听的对象中添加、更新、移除属性时,分别执行xxxAdded()、xxxReplaced()、xxxRemoved()方法。xxx分别代表Session、context、Request。

    package linstener;
    
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    public class SessionAttributeListenerTest implements HttpSessionAttributeListener {
        
        Log log=LogFactory.getLog(this.getClass());
        @Override
        public void attributeAdded(HttpSessionBindingEvent arg0) {
            
            //添加属性时被调用
            HttpSession session=arg0.getSession();
            String name=arg0.getName();
            log.info("新建session 属性:"+name+",值为:"+arg0.getValue());
        }
    
        @Override
        public void attributeRemoved(HttpSessionBindingEvent arg0) {
            //删除属性前被调用
            HttpSession session =arg0.getSession();
            String name=arg0.getName();
            log.info("删除session属性:"+name+",值为:"+arg0.getValue());
        }
    
        @Override
        public void attributeReplaced(HttpSessionBindingEvent arg0) {
            //修改属性时被调用
            HttpSession session =arg0.getSession();
            String name=arg0.getName();
            Object oldValue=arg0.getValue();
            log.info("修改session属性:"+name+", 原值:"+oldValue+",新值:"+session.getAttribute(name));
        }
    
    }

     web.xml 配置

    <listener>
          <listener-class>
              linstener.SessionAttributeListenerTest
    </listener-class>
    </listener>
  • 相关阅读:
    一个高级的makefile文件
    poj3616题(动态规划),看了别人的提示,自己又写了一遍
    浅谈C++ IO标准库(1)
    https证书安装踩坑
    一个简单通知服务的开发和搭建
    WCF学习笔记
    线程(Thread)、线程池(ThreadPool)技术
    BackgroundWorker与线程使用
    使用ITextSharp生成PDF文件心得
    值类型与引用类型
  • 原文地址:https://www.cnblogs.com/gengaixue/p/6730915.html
Copyright © 2011-2022 走看看