如何用Filter监听器统计浏览器在线访问人数

一、创建监听器

    //创建监听器
    public void sessionCreated(HttpSessionEvent se) {
        //统计Session中有多少个ID
        System.out.println(se.getSession().getId());

        ServletContext ctx = se.getSession().getServletContext();
        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCountLisener");
        if (onlineCount == null){
            onlineCount = new Integer(1);
        }else {
            int count = onlineCount.intValue();

            onlineCount =  new Integer((count + 1));
        }
        ctx.setAttribute("OnlineCountLisener",onlineCount);

    }

二、销毁监听器

    //销毁监听器
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();
        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCountLisener");
        if (onlineCount == null){
            onlineCount = new Integer(0);
        }else {
            int count = onlineCount.intValue();

            onlineCount =  new Integer((count - 1));
        }
        ctx.setAttribute("OnlineCountLisener",onlineCount);
    }

  • 完整后台代码如下:

注意监听器要实现的接口是:HttpSessionListener;注意重写其方法。

//统计在线人数,统计Session
public class OnlineCountLisener implements HttpSessionListener {

    //创建监听器
    public void sessionCreated(HttpSessionEvent se) {
        //统计Session中有多少个ID
        System.out.println(se.getSession().getId());

        ServletContext ctx = se.getSession().getServletContext();
        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCountLisener");
        if (onlineCount == null){
            onlineCount = new Integer(1);
        }else {
            int count = onlineCount.intValue();

            onlineCount =  new Integer((count + 1));
        }
        ctx.setAttribute("OnlineCountLisener",onlineCount);

    }

    //销毁监听器
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();
        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCountLisener");
        if (onlineCount == null){
            onlineCount = new Integer(0);
        }else {
            int count = onlineCount.intValue();

            onlineCount =  new Integer((count - 1));
        }
        ctx.setAttribute("OnlineCountLisener",onlineCount);
    }
}


三、编写前端页面

前端页面统计浏览器在线人数

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h1>
    当前有 <span style="color: aquamarine">
    <%=this.getServletConfig().getServletContext().getAttribute("OnlineCountLisener")%></span> 人在线
</h1>
</body>
</html>


四、配置XML

<!--注册监听器-->
    <listener>
        <listener-class>com.xiaomu.listener.OnlineCountLisener</listener-class>
    </listener>

五、运行效果

在这里插入图片描述

注意看Session的ID,刚刚开始运行的时候并非是一个ID

在这里插入图片描述


最初启动Tomcat时,SessionID并非只有一个,因为启动时Tomcat会不断和浏览器建立连接,有可能连接失败但SessionID已经创建了


在这里插入图片描述
在这里插入图片描述

  • 当然,换一个浏览器访问的话SessionID也会新创建
  • 但在同一个浏览器访问的话,SessionID是不会变的,即不会创建新的SessionID
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧木XIAOMU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值