本文共 1146 字,大约阅读时间需要 3 分钟。
import javax.servlet.ServletContext;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class OnlineListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent se) { HttpSession session = se.getSession(); if(session.isNew()){ ServletContext context = session.getServletContext(); //第几个访问者 Integer caller = (Integer) context.getAttribute("caller"); //在线人数 Integer counter = (Integer) context.getAttribute("counter"); if(caller==null){ caller = 1; }else{ caller++; } if(counter==null){ counter = 1; }else{ counter++; } context.setAttribute("counter",counter); session.setAttribute("caller",counter); } } public void sessionDestroyed(HttpSessionEvent se) { }}
<%@ page language="java" pageEncoding="UTF-8"%> 你是第${caller}访问者 当前在线${counter}人
转载于:https://blog.51cto.com/357712148/2105772