前言
感谢
Listener介绍
Listener,监听器,作为监听web的事件监听
Listener在JavaWeb中,是使用观察者设计模式进行实现的。在这个模式下有两个角色:观察者和被观察者
观察者在被观察者中注册自己感兴趣的事件,当这个事件发生时,会调用接口来通知观察者。
事件监听机制
1、事件:要监听的事情
2、事件源:事件发生的地方
3、监听器:一个对象,用于监听事件
4、注册监听:将事件,事件源,监听器绑定在一起。当事件源发生事件之后,执行监听器代码
快速起步
1、定义一个类,实现ServletContextListener接口
2、复写方法
package com.howling.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class HelloListener implements ServletContextListener {
/**
* ServletContext对象服务器启动后自动创建,这个是监听ServletContext而创建的,在服务器启动之后会自动调用
*
* @param servletContextEvent
*/
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("servletContextListener.contextInitialized...");
}
/**
* 服务器关闭之后,ServletContext对象被销毁,只有当服务器正常关闭之后,该方法才会调用
*
* @param servletContextEvent
*/
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("servletContextListener.contextDestroyed...");
}
}
3、在web.xml中进行注册
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="false">
<listener>
<listener-class>com.howling.listener.HelloListener</listener-class>
</listener>
</web-app>
4、启动服务器,查看结果
Listener详解
Servlet3.0为我们提供了八个监听器接口,根据作用域分为三类
1、Servlet上下文监听接口
- ServletContextListener
- ServletContextAttributeListener
2、HTTP Session监听接口
- HttpSessionListener
- HttpSessionActivationListener
- HttpSessionAttributeListener
- HttpSessionBindingListener
3、Servlet Request监听接口
- ServletRequestListener
- ServletRequestAttributeListener
Servlet上下文监听接口
ServletContextListener
Servlet上下文监听器接口,用于监听上下文初始化和上下文即将关闭事件。
里面有两个方法
- contextInitialized:上下文初始化
- contextDestroyed:上下文即将关闭
当对应的条件满足时,会自动执行这两个方法 上下文初始化和即将关闭,也就是在服务器开启和即将关闭的时刻,上下文会自动创建和销毁
ServletContextAttributeListener
表示Servlet上下文属性相关事件,当属性发生改变时及聚会触发,分为三个方法
- attributeAdded:增加
- attributeRemoved:删除
- attributeReplaced:修改
替换方法注意 这里假如属性的值被替换的时候回调,那么ServletContextAttributeEvent.getValue()是之前的值
HTTP Session监听接口
HttpSessionListener
Session被创建和销毁的时候,回调方法
- sessionCreated
- sessionDestoryed
HttpSessionActivationListener
session即将钝化或者被激活时,回调方法
- sessionWillPassivate
- sessionDidActivate
Session的钝化和激活我们曾经在 Cookie&Session 的那篇文章讲过
HttpSessionAttributeListener
当Session发生增删改的时候回调方法
- attributeAdded
- attributeRemoved
- attributeReplaced
替换方法注意 这里假如属性的值被替换的时候回调,那么ServletContextAttributeEvent.getValue()是之前的值
HttpSessionBindingListener
当Session属性发生增删时,也就是属性值绑定和解绑的时候,回调方法
- valueBound
- valueUnbound
当调用session的invalidate方法或者session失效时,也会回调valueUnbound方法。
Servlet Request监听接口
ServletRequestListener
当请求初始化或者销毁时,即客户端请求进入web应用(进入servlet或者第一个filter),回调方法
- requestInitialized
- requestDestoryed
ServletRequestAttributeListener
当请求的属性发生增删改的时候,servlet容器构造ServletRequestAttribute事件对象,并回调
- attributeAdded
- attributeRemoved
- attributeReplaced