想法
既然需要监听session
我们就需要监听器,我们就需要自定义实现HttpSessionListener
接口
然后我们定义一个用来存在线人数的类OnlineCount
,变量为静态方法也为静态
实现HttpSessionListener
接口的TestSessionListener
重写了sessionCreated
和sessionDestroyed
方法,这两个方法分别是在session创建和销毁时执行,所以我们每次在运行方法时调整OnlineCount
中变量,然后写一个jsp界面专门让这个变量输出即可
最后记得,要在web.xml
里面注册一下监听器
如果看到启动tomcat却出现两个session,是因为IDEA它也会链接一个session
然后我还弄了一个网站自动刷新的东西
OnlineCount
package top.sehnsucht;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/9/26
* ~~(^_^)~~
*/
public class OnlineCount {
private static Integer onlineNum = 0;
public static void add() {
onlineNum++;
System.out.println("add");
System.out.println(onlineNum);
}
public static void delete() {
onlineNum--;
System.out.println("delete");
System.out.println(onlineNum);
}
public static Integer getOnlineNum() {
return onlineNum;
}
}
TestSessionListener
package top.sehnsucht;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/9/26
* ~~(^_^)~~
*/
public class TestSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
OnlineCount.add();
se.getSession().setMaxInactiveInterval(1000);//秒为单位
HttpSessionListener.super.sessionCreated(se);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
OnlineCount.delete();
HttpSessionListener.super.sessionDestroyed(se);
}
}
showOnlineNum.jsp
<%@ page import="top.sehnsucht.OnlineCount" %><%--
Created by IntelliJ IDEA.
User: windows
Date: 2021/9/26
Time: 16:39
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>
<%
out.println("现在在线人数是" + OnlineCount.getOnlineNum());
response.setHeader("refresh", "3");
%>
</body>
</html>
web.xml
<!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 version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>top.sehnsucht.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>loginTime</servlet-name>
<servlet-class>top.sehnsucht.CookieLoginTime</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginTime</servlet-name>
<url-pattern>/time</url-pattern>
</servlet-mapping>
<listener>
<listener-class>top.sehnsucht.TestSessionListener</listener-class>
</listener>
</web-app>
参考:
https://blog.csdn.net/gsjwxhn/article/details/90707571
https://blog.csdn.net/weixin_40747272/article/details/79655775
https://blog.csdn.net/lovexuwenhao/article/details/83395642?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-1.no_search_link&spm=1001.2101.3001.4242
感谢参与测试的cjb,lxl,ly,ljl