监听器学习:

问题:

在 Servlet 技 术 中 我 们 学 习 了 request 、 session 、application 作用域对象,其主要作用是实现数据的在不同场景中的灵活流转。但是数据的具体流转过程我们是看不到的,比如作用域对象是什么时候创建和销毁的,数据是什么时候存取,改变和删除的。因为具体的流转过程看不到,所以也就无法再指定的时机对数据和对象进行操作,比如 session 销毁的时候,在线人数-1。

解决:

使用监听器

概念:

Servlet 监听器是 Servlet 规范中定义的一种特殊类,用于监听 ServletContext、HttpSession 和 ServletRequest 等域对象的创建与销毁事件,以及监听这些域对象中属性发生修改的事件。

监听对象:

Request

Session

Application

监听内容:

创建、销毁、属性改变事件

监听作用:

在事件发生之前,之后要进行一些处理,比如统计在线人数

使用:

1.创建一个普通Java类实现指定的接口

监听request的创建和销毁:ServletRequestListener

requestInitialized(ServletRequestEvent sre)

requestDestroyed(ServletRequestEvent sre)

形参:

ServletRequestEvent可以获取当前监听到的request对象,对request对象中的资源进行操作。

监听request作用域数据的变更:ServletRequestAttributeListener

attributeAdded(ServletRequestAttributeEvent srae)

attributeRemoved(ServletRequestAttributeEvent srae)

attributeReplaced(ServletRequestAttributeEvent srae)

形参:

ServletRequestAttributeEvent可以获取当前被监听到的request中的数据。

getName()返回监听到的数据的键和getValue()返回监听到的数据的值。

监听session的创建和销毁:HttpSessionListener

sessionCreated(HttpSessionEvent se)

sessionDestroyed(HttpSessionEvent se)

形参:

获取当前被监听到的session对象

监听session的作用域数据的变更:

attributeAdded(HttpSessionBindingEvent event)

attributeRemoved(HttpSessionBindingEvent event)

attributeReplaced(HttpSessionBindingEvent event)

形参:

获取当前监听到的session中的数据 getName()返回数据的键名,getValue()返回数据的值

监听application对象对象的创建和销毁:ServletContextListener

contextDestroyed(ServletContextEvent sce)

contextInitialized(ServletContextEvent sce)

形参:

获取当前被监听到的application对象

监听application的作用域数据的变更:ServletContextAttributeListener

attributeAdded(ServletContextAttributeEvent event)

attributeRemoved(ServletContextAttributeEvent event)

attributeReplaced(ServletContextAttributeEvent event)

形参:

获取当前被监听的数据 getName()返回数据的键名,getValue()返回数据的值

2.在项目的web.xml中配置监听器,让监听器生效

监听器类的包名和类名

示例:

com.bj.listener.MyListener

案例:

统计网站在线人数。

显示网站当前在线人数:

功能分析:

统计当前系统的session的个数,一个session意味一个在线的用户。

在session被创建的时候使用计数器+1,session被销毁的时候计数器-1,

将计数器存储在ServletContext对象。

问题:

我怎么能在session创建的时候触发一个计数器进行计数呢?

解决:

使用监听器

使用:

创建一个普通java类实现Session监听接口和application监听接口。

代码实现:

1.创建一个普通java类实现Session监听接口和application监听接口。

package com.bj.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyListener implements HttpSessionListener,ServletContextListener{
//监听Application对象@Override
public void contextInitialized(ServletContextEvent sce) {
int count=0;
//获取Application对象ServletContext sc = sce.getServletContext();
sc.setAttribute("count",count);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
//监听Session对象@Override
public void sessionCreated(HttpSessionEvent se) {
//获取Application对象中的计数器ServletContext sc = se.getSession().getServletContext();
int count=(int) sc.getAttribute("count");
//计数器自增++count;
//然后再将计数器存储到application中sc.setAttribute("count", count);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
// 获取Application对象中的计数器ServletContext sc = se.getSession().getServletContext();
int count=(int) sc.getAttribute("count");
//计数器自增--count;
//然后再将计数器存储到application中sc.setAttribute("count", count);
}
}

2.web.xml中配置监听器:

com.bj.listener.MyListener1

main.jsp是用户登录网页后的主页面,这里模拟网页当前页面人数展示的模块

pageEncoding="UTF-8"%>

Insert title here


当前在线人数为:${applicationScope.count}



用户登录后在线人数用户使用其他浏览器登录后的在线人数