监听器 Listener
监听事件发生,在事件发生前后能够做出相应处理的 web 应用组件。
Listener实现了 javax.servlet.ServletContextListener 接口的服务器端程序;随web应用的启动而启动,只初始化一次,随web应用的停止而销毁。且并不是直接注册在事件源上,而是由 servlet 容器负责注册,开发只需在部署描述符中进行配置,servlet 容器会自动将监听器注册到事件源中。
主要作用是:做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等。
web 监听器的分类
按照监听对象划分:
- 监听应用程序环境(ServletContext)
- ServletContextListener
- ServletContextAttributeListener
- 监听用户请求对象(ServletRequest)
- ServletRequestListener
- ServletRequestAttributeListener
- 监听用户会话对象(HTTPSession)
- HttpSessionListener
- HTTPSessionAttributeListener
- HttpSessionActivationListener
- HttpSessionBindingListener
对对象的创建和销毁的监听
ServletContextListener、ServletRequestListener、HttpSessionListener
对对象属相的监听
ServletContextAttributeListener、ServletRequestAttributeListener、HTTPSessionAttributeListener
监听 Session 持久化到磁盘或从磁盘中重新加载到 JVM 中的监听器
HttpSessionActivationListener
监听 Session 绑定和移除绑定的监听器
HttpSessionBindingListener
监听器的应用场景
- 应用统计(针对 session 创建进行监听:用户登录统计
- 任务触发
- 业务需求
监听器的启动顺序
与过滤器一致,依赖于在部署描述符的注册顺序。
监听器、过滤器、servlet 的启动顺序
监听器 -> 过滤器 -> Servlet
监听器的创建
在 web.xml 中创建 Listener
监听器的创建只有一个子元素。在 与 节点之间。
TestListener
java 代码中创建 listener 继承类
listener 类
@WebListener()
public class TestListener implements ServletContextListener,
ServletRequestListener, HttpSessionAttributeListener {
// Public constructor is required by servlet spec
public TestListener() {
}
// -------------------------------------------------------
// ServletContextListener implementation
// -------------------------------------------------------
public void contextInitialized(ServletContextEvent sce) {
/* This method is called when the servlet context is
initialized(when the Web application is deployed).
You can initialize servlet context related data here.
*/
System.out.println("listener: context init");
}
public void contextDestroyed(ServletContextEvent sce) {
/* This method is invoked when the Servlet Context
(the Web application) is undeployed or
Application Server shuts down.
*/
System.out.println("listener: context destroy");
}
public void attributeAdded(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is added to a session.
*/
System.out.println("listener: session attribute added");
}
public void attributeRemoved(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is removed from a session.
*/
System.out.println("listener: session attribute removed");
}
public void attributeReplaced(HttpSessionBindingEvent sbe) {
/* This method is invoked when an attibute
is replaced in a session.
*/
System.out.println("listener: session attribute replaced");
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("listener: request destroy");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("listener: request init");
}
}
在其他类中调用
调用 HttpSessionAttributeListener 方法。
if (name != null) {
System.out.println("second login: " + name);
session.removeAttribute("userName");//移除 attribue 调用 HttpSessionAttributeListener 的attributeRemoved 方法
}
session.setAttribute("userName", userName);
实例讲解
在登录的时候新增了 Attribute,并在第二次登录的时候移除;且登录的同时也会新增 Attribute;