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>
  • 相关阅读:
    电容的串联和并联的性质
    start.sh
    shell 得到当前目录路径
    Java程序远程无法执行nohup命令
    mysql windows 安装5.7
    电阻并联的性质
    电阻串联的性质
    webjars
    邮箱设置
    万用表使用注意事项
  • 原文地址:https://www.cnblogs.com/gengaixue/p/6730915.html
Copyright © 2011-2022 走看看