Implementation ideas
The common process is that the standard mvc is the login form. The user submits the data to the login check. If the login check is passed, the session event will be triggered, saved and entered the online personnel list. The page will jump to the online user list. If the user logs out, it will be deleted from the online list
The code is as follows
Using set set, i.e. set set set de duplication causes are internally stored as map, and the key value pair of mqp is hashcode. Because of the characteristics of hash table, i.e. set de duplication
Project structure
Create iterator
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; // Listening to servlet context public class OnlineUserList implements HttpSessionAttributeListener, HttpSessionListener, ServletContextListener { private ServletContext servletContext = null; // When the session attribute is added, the event is triggered // session attribute added @Override public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) { Set all = (Set)this.servletContext.getAttribute("online"); all.add(httpSessionBindingEvent.getValue()); this.servletContext.setAttribute("online", all); } // User logout login // session property delete @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) { } // Context initialization // Initialization @Override public void contextInitialized(ServletContextEvent servletContextEvent) { // Get context chestnut this.servletContext = servletContextEvent.getServletContext(); // Set save set set this.servletContext.setAttribute("online", new TreeSet()); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { } // session destroy @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { Set all = (Set)this.servletContext.getAttribute("online"); all.remove(httpSessionEvent.getSession().getAttribute("id")); this.servletContext.setAttribute("online", all); } }
configuration file
<!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>
Online user statistics
<%@ 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 a.m.: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> //You are signed in //Show users online <% Set all = (Set)super.getServletContext().getAttribute("online"); Iterator iterator = all.iterator(); while(iterator.hasNext()){ %> <%=iterator.next()%> <% } %> </body> </html>