18、监听器
实现一个监听器的接口;(有n种监听器)
-
编写一个监听器
实现监听器的接口…
依赖的jar包
demo
package com.myweb.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineCountListener implements HttpSessionListener {
//创建session监听: 看你的一举一动
//一旦创建Session就会触发一次这个事件!
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
//拿到上下文对象,用于存储
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
//监视一个sesionid
System.out.println(httpSessionEvent.getSession().getId());
//记录一下访问个数
Integer integer = (Integer) servletContext.getAttribute("OnlineCount");;
if (integer == null)
{
integer = 1;//自动装箱 等价于integer = new Integer(1);
}else {
int i = integer;//自动拆箱
integer = ++i ;//自动装箱
}
//存到上下文中
servletContext.setAttribute("OnlineCount",integer);
}
//销毁session监听
//一旦销毁Session就会触发一次这个事件!
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
//拿到上下文对象,用于存储
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
//监视一个sesionid
System.out.println(httpSessionEvent.getSession().getId());
//记录一下访问个数
Integer integer = (Integer) servletContext.getAttribute("OnlineCount");;
if (integer == null)
{
integer = 1;//自动装箱 等价于integer = new Integer(1);
}else {
int i = integer;//自动拆箱
integer = --i ;//自动装箱
}
//存到上下文中
servletContext.setAttribute("OnlineCount",integer);
}
}
看不懂integer拆装箱的。用int类型
package com.myweb.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineCountListener implements HttpSessionListener {
//创建session监听: 看你的一举一动
//一旦创建Session就会触发一次这个事件!
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
//拿到上下文对象,用于存储
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
//监视一个sesionid
System.out.println(httpSessionEvent.getSession().getId());
//记录一下访问个数
int i = 0;
if (servletContext.getAttribute("OnlineCount") == null)
{
i = 1;
}else {
i = (int) servletContext.getAttribute("OnlineCount");
i++;
}
//存到上下文中
servletContext.setAttribute("OnlineCount",i);
}
//销毁session监听
//一旦销毁Session就会触发一次这个事件!
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
//拿到上下文对象,用于存储
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
//监视一个sesionid
System.out.println(httpSessionEvent.getSession().getId());
//记录一下访问个数
int i = 0;
if (servletContext.getAttribute("OnlineCount") == null)
{
i = 1;
}else {
i = (int) servletContext.getAttribute("OnlineCount");
i++;
}
//存到上下文中
servletContext.setAttribute("OnlineCount",i);
}
}
<listener>
<listener-class>com.myweb.listener.OnlineCountListener</listener-class>
</listener>
前端查看
<%--
Created by IntelliJ IDEA.
User: xubainan
Date: 2021/3/11
Time: 4:56 下午
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>读取在线人数</title>
</head>
<body>
当前在线<%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%>人
</body>
</html>