监听器实栗 在线人数统计

实现思路

常见的流程是,标准的mvc 即 登录表单,用户提交数据到登录检查,若登录检查通过以后,触发session事件,保存进入在线人员列表中,页面跳转到在线用户列表,若用户注销,从在线列表中删除.

代码如下

使用set集合, 即 set集合去重 原因 内部存储为map,mqp的键值对为hashcode 由于哈希表的特征 即 set可去重

项目结构

创建迭代器

package com.ming.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Set;
import java.util.TreeSet;

// 对servlet 上下文监听
public class OnlineUserList implements HttpSessionAttributeListener, HttpSessionListener, ServletContextListener {
    private ServletContext servletContext = null;
    // 增加session属性的时候,触发事件
    // session 属性增加
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        Set all = (Set)this.servletContext.getAttribute("online");
        all.add(httpSessionBindingEvent.getValue());
        this.servletContext.setAttribute("online", all);
    }

    // 用户注销登录
    // session 属性删除
    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        Set all = (Set)this.servletContext.getAttribute("online");
        all.remove(httpSessionBindingEvent.getValue());
        this.servletContext.setAttribute("online", all);
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    // 上下文初始化
    // 初始化
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // 获得上下文实栗
        this.servletContext = servletContextEvent.getServletContext();
        // 设置保存set集合
        this.servletContext.setAttribute("online", new TreeSet());
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    // session 销毁
    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        Set all = (Set)this.servletContext.getAttribute("online");
        all.remove(httpSessionEvent.getSession().getAttribute("id"));
        this.servletContext.setAttribute("online", all);
    }
}

配置文件




  Archetype Created Web Application
    
        com.ming.listener.OnlineUserList
    
    
        LoginFile
        com.ming.filter.LoginFile
    
    
        LoginFile
        /index.jsp
    
    
        login
        com.ming.servlrt.LoginServlet
    
    
        login
        /loginServlet
    


在线用户统计

<%@ page import="java.util.Set" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="javax.swing.text.html.HTMLDocument" %><%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-17
  Time: 上午4:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


您已经登录
显示用户在线
<%
    Set all = (Set)super.getServletContext().getAttribute("online");
    Iterator iterator = all.iterator();
    while(iterator.hasNext()){
        %>
            <%=iterator.next()%>
        <%
    }
%>



运行效果

你可能感兴趣的:(后端)