實現思路
常見的流程是,標準的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);
}
}
配置文件
/p>
"-//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>
<filter>
<filter-name>LoginFile/<filter-name>
<filter-class>com.ming.filter.LoginFile/<filter-class>
<filter-mapping>
<filter-name>LoginFile/<filter-name>
<url-pattern>/index.jsp/<url-pattern>
<servlet>
<servlet-name>login/<servlet-name>
<servlet-class>com.ming.servlrt.LoginServlet/<servlet-class>
<servlet-mapping>
<servlet-name>login/<servlet-name>
<url-pattern>/loginServlet/<url-pattern>
在線用戶統計
Created by IntelliJ IDEA.
User: ming
Date: 19-3-17
Time: 上午4:14
To change this template use File | Settings | File Templates.
--%>
<title>Title/<title>
您已經登錄
顯示用戶在線
Set all = (Set)super.getServletContext().getAttribute("online");
Iterator iterator = all.iterator();
while(iterator.hasNext()){
%>
}
%>
運行效果
閱讀更多 mySoul2018 的文章