实现思路

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

代码如下

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

项目结构

2019-03-17-23-44-20----

创建迭代器

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);
    }
}

配置文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
    <listener>
        <listener-class>com.ming.listener.OnlineUserList</listener-class>
    </listener>
    <filter>
        <filter-name>LoginFile</filter-name>
        <filter-class>com.ming.filter.LoginFile</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFile</filter-name>
        <url-pattern>/index.jsp</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>com.ming.servlrt.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/loginServlet</url-pattern>
    </servlet-mapping>
</web-app>

在线用户统计

<%@ 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" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
您已经登录
显示用户在线
<%
    Set all = (Set)super.getServletContext().getAttribute("online");
    Iterator iterator = all.iterator();
    while(iterator.hasNext()){
        %>
            <%=iterator.next()%>
        <%
    }
%>
</body>
</html>

运行效果

2019-03-17-23-47-09----