统计在线人数--监听器

统计在线人数–监听器


/*
 *  统计网站登录的在线人数:
 *      session每创建一次,证明有一个用户访问了网站(统计网站访问的在线人数)
 *      session中每有一个属性增加的时候,证明有一个用户登录了网站(统计网站登录的在线用户)
 *
 *      思路:
 *          1.在servletContext创建的时候,可以在servletContxt中设置一个count属性用来统计网站当前登录的用户数量
 *          2.设置一个session属性添加监听器,当每次session中有属性新增的时候,就从servletContext中将count取出来,+1再放回去
 *          3.在session销毁的时候,把session中设置的user属性移除
 *          4.设置一个session属性移除监听器,监听到属性移除的时候,将count从context中拿出来并-1放回去
 */
//ServletContextListener如果在web.xml中配置了这个监听器,那么启动容器时,就会默认执行它实现的方法,它用于对Servlet整个上下文进行监听(创建、销毁)
//HttpSessionListener接口是对Session的整体状态(创建,销毁)进行监听。
//HttpSessionAttributeListener是对session的属性(添加,删除,替换)进行监听。
@WebListener()
public class MyListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        /*
         * 通过httpSessionBindingEvent.getSession()获得session对象
         * */
        HttpSession httpSession = httpSessionBindingEvent.getSession();
        /*
         * 通过session获得当前的context对象
         * */
        ServletContext servletContext = httpSession.getServletContext();
        int count = (int)servletContext.getAttribute("count");
        servletContext.setAttribute("count",count+1);
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        HttpSession httpSession = httpSessionBindingEvent.getSession();
        ServletContext servletContext = httpSession.getServletContext();
        int count = (int)servletContext.getAttribute("count");
        servletContext.setAttribute("count",count-1);
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("servletContext初始化");
        /*
         * 通过servletContextEvent.getServletContext()获得servletContext
         * */
        ServletContext servletContext = servletContextEvent.getServletContext();
        /*
         * 在服务器启动的时候,在context中设置一个count属性用来统计人数,初始值为0
         * */
        servletContext.setAttribute("count",0);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession httpSession = httpSessionEvent.getSession();
        /*
         * 从session中将user拿出来,如果user为空,证明当前的用户没有登录,如果user不为空,证明当前的这个session是登陆过的
         * */
        Object username = httpSession.getAttribute("username");
        Object password = httpSession.getAttribute("password");
        Object id = httpSession.getAttribute("id");

        if (username!=null && password!=null && id != null){
            httpSession.removeAttribute("username");
            httpSession.removeAttribute("password");
            httpSession.removeAttribute("id");
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值