0%
这是一片思考的空间 -- arthinking
Spring 重构&代码整洁之道 软件设计 JVM 并发编程 数据结构与算法 分布式 存储 网络 微服务 设计模式
Java技术栈 - 涉及Java技术体系

Java Web笔记 - Servlet中的Listener监听器的介绍 常用监听器接口 实现监听器

Listener是Servlet的监听器,它可以监听客户端的请求,服务器端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户数量。

比如,当增加一个HttpSession时,就自动触发sessionCreated(HttpSessionEvent se)方法,在这个方法中就可以统计在线人数了。

使用到了观察者模式。

1、常用的监听接口:

1.1、ServletContextAttributeListener

监听ServletContext属性的操作,比如增加,删除或修改属性。

其中每个方法都提供了一个监听事件类:ServletContextAttributeEvent可以从这个类中获取被监听到的属性的名称和值。

attributeAdded
void attributeAdded(ServletContextAttributeEvent scab)

Notification that a new attribute was added to the servlet context. Called after the attribute is added.

attributeRemoved
void attributeRemoved(ServletContextAttributeEvent scab)

Notification that an existing attribute has been removed from the servlet context. Called after the attribute is removed.

attributeReplaced
void attributeReplaced(ServletContextAttributeEvent scab)

Notification that an attribute on the servlet context has been replaced. Called after the attribute is replaced.

1.2、ServletContextListener

当创建ServletContext对象时,激发contextInitialized方法;当销毁ServletContext对象时,激发contextDestroyed方法。

1.3、HttpSessionListener

当创建一个Session对象时,激发sessionCreated事件;当销毁一个Session对象时,激发sessionDestroyed事件。

sessionCreated
void sessionCreated(HttpSessionEvent se)

Notification that a session was created.
Parameters:
se - the notification event

sessionDestroyed
void sessionDestroyed(HttpSessionEvent se)

Notification that a session is about to be invalidated.
Parameters:
se - the notification event

HttpSessionEvent事件类:

public class HttpSessionEvent
extends EventObject

This is the class representing event notifications for changes to sessions within a web application.

相关方法:

getSession
public HttpSession getSession()

Return the session that changed.

1.4、HttpSessionAttributeListener

监听HttpSession中的属性操作,在HttpSession中添加一个属性时,激发attributeAdded方法;当在Session中删除一个属性时,激发attributeRemoved方法;当Session属性被重新设置时,激发attributeReplaced方法。

2、一个监听器的实现步骤:

这里编写一个ServletContextAttributeListener的实现类:

public class MyServletContextAttributeListener implements
ServletContextAttributeListener {

@Override
public void attributeAdded(ServletContextAttributeEvent scab) {
    System.out.println("添加属性:" \+ scab.getName() + scab.getValue());
}

@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {
    System.out.println("删除属性:" \+ scab.getName() + scab.getValue());
}

@Override
public void attributeReplaced(ServletContextAttributeEvent scab) {
    System.out.println("修改属性" \+ scab.getName() + scab.getValue());
}

}

在web.xml部署描述符中部署:

com.itzhai.listener.MyServletContextAttributeListener

注意:监听器一般配置在拦截器的前面。

这样当有JSP或者Servlet添加,删除或修改了ContextAttribute的属性时就会触发相应的事件:

<%
//触发添加事件
application.setAttribute("username", "arthinking");
//触发修改事件
application.setAttribute("username", "Jason");
%>

在开发中一般都是由框架实现这些接口。

欢迎关注我的其它发布渠道

订阅IT宅
内功修炼
Java技术栈
Java架构杂谈是IT宅精品文章公众号,欢迎订阅:
📄 网络基础知识:两万字长文50+张趣图带你领悟网络编程的内功心法 📄 HTTP发展史:三万长文50+趣图带你领悟web编程的内功心法 📄 HTTP/1.1:可扩展,可靠性,请求应答,无状态,明文传输 📄 HTTP/1.1报文详解:Method,URI,URL,消息头,消息体,状态行 📄 HTTP常用请求头大揭秘 📄 HTTPS:网络安全攻坚战 📄 HTTP/2:网络安全传输的快车道 📄 HTTP/3:让传输效率再一次起飞 📄 高性能网络编程:图解Socket核心内幕以及五大IO模型 📄 高性能网络编程:三分钟短文快速了解信号驱动式IO 📄 高性能网络编程:彻底弄懂IO复用 - IO处理杀手锏,带您深入了解select,poll,epoll 📄 高性能网络编程:异步IO:新时代的IO处理利器 📄 高性能网络编程:网络编程范式 - 高性能服务器就这么回事 📄 高性能网络编程:性能追击 - 万字长文30+图揭秘8大主流服务器程序线程模型
📄 Java内存模型:如果有人给你撕逼Java内存模型,就把这些问题甩给他 📄 一文带你彻底理解同步和锁的本质(干货) 📄 AQS与并发包中锁的通用实现 📄 ReentrantLock介绍与使用 📄 ReentrantReadWriteLock介绍与使用 📄 ReentrantLock的Condition原理解析 📄 如何优雅的中断线程 📄 如何优雅的挂起线程 📄 图解几个好玩的并发辅助工具类 📄 图解BlockingQueue阻塞队列
📄 消息队列那么多,为什么建议深入了解下RabbitMQ? 📄 高并发异步解耦利器:RocketMQ究竟强在哪里? 📄 Kafka必知必会18问:30+图带您看透Kafka
📄 洞悉MySQL底层架构:游走在缓冲与磁盘之间 📄 SQL运行内幕:从执行原理看调优的本质 📄 洞悉Redis技术内幕:缓存,数据结构,并发,集群与算法