监听器

创建一个类实现两个接口HttpSessionListener, HttpSessionAttributeListener并且是现在他们的方法

@WebListener()
public class HttpSessionListenerUtil implements HttpSessionListener, HttpSessionAttributeListener {

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    // 监听session何时创建
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
        System.out.println("session 创建了 。。。。。。。");
        System.out.println("------------" +se.getSession().getId());
        System.out.println("---------" + se.getSession().getAttribute("session_user"));
        System.out.println("==========================================================");
    }

    // 监听session销毁的时机
    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
        System.out.println(" session 销毁了。。。。。。。");
        System.out.println(" *************" + se.getSession().getAttribute("session_user"));
        System.out.println("========================================");
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------
    // 监听session域中何时存值
    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
        System.out.println(" session域中 添加属性值了。。。。。");
        // 获取放进session域中的key值
        System.out.println("key值: " + sbe.getName());
        // 获取放进session域中的value值
        System.out.println("value值: " + sbe.getValue());
        System.out.println("================================");
    }

    // 监听 删除从session域中存储的值
    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
        System.out.println("session 域中 开始删除值了。。。。。");
        System.out.println(sbe.getName());
        System.out.println(sbe.getValue());
        System.out.println("===================================");
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attribute
         is replaced in a session.
      */
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值