Servlet事件监听器
一、监听器概念
1、事件源:发生事件的对象。
2、监听器:是一个接口,监听事件源上要发生的动作
3、事件:事件对象一般是作为监听器方法的参数存在的,它封装了发生事件的对象
例:
------------------------------------------------------------------------------------------------------
// 事件源 观察者设计模式
public class Student {
private String name;
private StudentListener listener;
public Student(String name) { // 注入: 构造方法注入
this.name = name;
}
// 注册监听
public void addStudentListener(StudentListener listener) {
this.listener = listener;
}
public String getName() {
return name;
}
public void eat() {
if (listener != null) {
listener.preEat(new StudentEvent(this));
}
System.out.println("开始吃");
}
public void study() {
if (listener != null) {
listener.preStudy(new StudentEvent(this));
}
System.out.println("开始学");
}
}
------------------------------------------------------------------------------------------------------
// 事件
public class StudentEvent {
private Object source;
public StudentEvent(Object source){
this.source = source;
}
// 获取事件源
public Object getSource(){
return source;
}
}
------------------------------------------------------------------------------------------------------
// 监听器
public interface StudentListener {
void preEat(StudentEvent e); // 监听吃
void preStudy(StudentEvent e); // 监听学
}
------------------------------------------------------------------------------------------------------
// 测试
public class StudentTest {
public static void main(String[] args) {
Student s = new Student("小明");
// 注册监听器
s.addStudentListener(new StudentListener() {
public void preStudy(StudentEvent e) {
Student ss = (Student) e.getSource();
System.out.println(ss.getName()+"学习前吃个水果");
}
public void preEat(StudentEvent e) {
Student ss = (Student) e.getSource();
System.out.println(ss.getName()+"饭前先喝杯奶");
}
});
s.study();
s.eat();
}
}
------------------------------------------------------------------------------------------------------
二、监听器编写步骤:
1、编写一个类实现某个监听器接口
2、在web.xml中注册监听器
<listener>
<listener-class>cn.itcast.listener.ServletContextDemoListener</listener-class>
</listener>
三、Servlet中提供的八个监听器(8个)
6.1、 监听ServletContext、HttpSession、ServletRequest对象的创建和销毁的监听器。
① ServletContextListener: 监听ServletContext对象的创建和销毁。
public void contextInitialized(ServletContextEvent sce) {} :
当ServletContext对象被创建时执行,仅执行一次 ,因此,可以把系统的一些初始化工作代码写在此处
public void contextDestroyed(ServletContextEvent sce) {}:
当ServletContext对象被销毁时执行
② HttpSessionListener: 监听HttpSession对象的创建和销毁。
创建:第一次调用request.getSession()时。
销毁:1、主动调用invalidate()方法 2、超时
public void sessionCreated(HttpSessionEvent se) {} : HttpSession对象被创建时调用
public void sessionDestroyed(HttpSessionEvent se) {}: HttpSession对象被销毁时调用
③ ServletRequestListener: 监听ServletRequest对象的创建和销毁。
public void requestInitialized(ServletRequestEvent sre) {} : ServletRequest对象被创建时调用
public void requestDestroyed(ServletRequestEvent sre) {} : ServletRequest对象被销毁时调用
注: 应用场景: 统计每个资源的访问次数
6.2、 监听ServletContext、HttpSession、ServletRequest对象中域变化(新来的,替换的,删除的)的监听器。
④ ServletContextAttributeListener:
⑤ HttpSessionAttributeListener:
public void attributeAdded(HttpSessionBindingEvent se) {} : session中添加东西时被调用
public void attributeReplaced(HttpSessionBindingEvent se) {} : session中修改东西时被调用
public void attributeRemoved(HttpSessionBindingEvent se) {} : session中删除东西时被调用
⑥ ServletRequestAttributeListener:
6.3、 感知型监听器:谁实现了这些接口,谁就能感知自己被怎么着了。这种监听器不需要注册。
⑦ HttpSessionActivationListener:感知自己何时随着HttpSession对象钝化和活化
public void sessionWillPassivate(HttpSessionEvent se) {} : 对象被钝化时调用
public void sessionDidActivate(HttpSessionEvent se) {} : 对象被活化时调用
⑧ HttpSessionBindingListener: 感知自己何时被HttpSession对象绑了(绑在域中)和解绑了。
public void valueBound(HttpSessionBindingEvent event) {} : 对象被绑定时调用
public void valueUnbound(HttpSessionBindingEvent event) {} : 对象被解绑时调用
一、监听器概念
1、事件源:发生事件的对象。
2、监听器:是一个接口,监听事件源上要发生的动作
3、事件:事件对象一般是作为监听器方法的参数存在的,它封装了发生事件的对象
例:
------------------------------------------------------------------------------------------------------
// 事件源 观察者设计模式
public class Student {
private String name;
private StudentListener listener;
public Student(String name) { // 注入: 构造方法注入
this.name = name;
}
// 注册监听
public void addStudentListener(StudentListener listener) {
this.listener = listener;
}
public String getName() {
return name;
}
public void eat() {
if (listener != null) {
listener.preEat(new StudentEvent(this));
}
System.out.println("开始吃");
}
public void study() {
if (listener != null) {
listener.preStudy(new StudentEvent(this));
}
System.out.println("开始学");
}
}
------------------------------------------------------------------------------------------------------
// 事件
public class StudentEvent {
private Object source;
public StudentEvent(Object source){
this.source = source;
}
// 获取事件源
public Object getSource(){
return source;
}
}
------------------------------------------------------------------------------------------------------
// 监听器
public interface StudentListener {
void preEat(StudentEvent e); // 监听吃
void preStudy(StudentEvent e); // 监听学
}
------------------------------------------------------------------------------------------------------
// 测试
public class StudentTest {
public static void main(String[] args) {
Student s = new Student("小明");
// 注册监听器
s.addStudentListener(new StudentListener() {
public void preStudy(StudentEvent e) {
Student ss = (Student) e.getSource();
System.out.println(ss.getName()+"学习前吃个水果");
}
public void preEat(StudentEvent e) {
Student ss = (Student) e.getSource();
System.out.println(ss.getName()+"饭前先喝杯奶");
}
});
s.study();
s.eat();
}
}
------------------------------------------------------------------------------------------------------
二、监听器编写步骤:
1、编写一个类实现某个监听器接口
2、在web.xml中注册监听器
<listener>
<listener-class>cn.itcast.listener.ServletContextDemoListener</listener-class>
</listener>
三、Servlet中提供的八个监听器(8个)
6.1、 监听ServletContext、HttpSession、ServletRequest对象的创建和销毁的监听器。
① ServletContextListener: 监听ServletContext对象的创建和销毁。
public void contextInitialized(ServletContextEvent sce) {} :
当ServletContext对象被创建时执行,仅执行一次 ,因此,可以把系统的一些初始化工作代码写在此处
public void contextDestroyed(ServletContextEvent sce) {}:
当ServletContext对象被销毁时执行
② HttpSessionListener: 监听HttpSession对象的创建和销毁。
创建:第一次调用request.getSession()时。
销毁:1、主动调用invalidate()方法 2、超时
public void sessionCreated(HttpSessionEvent se) {} : HttpSession对象被创建时调用
public void sessionDestroyed(HttpSessionEvent se) {}: HttpSession对象被销毁时调用
③ ServletRequestListener: 监听ServletRequest对象的创建和销毁。
public void requestInitialized(ServletRequestEvent sre) {} : ServletRequest对象被创建时调用
public void requestDestroyed(ServletRequestEvent sre) {} : ServletRequest对象被销毁时调用
注: 应用场景: 统计每个资源的访问次数
6.2、 监听ServletContext、HttpSession、ServletRequest对象中域变化(新来的,替换的,删除的)的监听器。
④ ServletContextAttributeListener:
⑤ HttpSessionAttributeListener:
public void attributeAdded(HttpSessionBindingEvent se) {} : session中添加东西时被调用
public void attributeReplaced(HttpSessionBindingEvent se) {} : session中修改东西时被调用
public void attributeRemoved(HttpSessionBindingEvent se) {} : session中删除东西时被调用
⑥ ServletRequestAttributeListener:
6.3、 感知型监听器:谁实现了这些接口,谁就能感知自己被怎么着了。这种监听器不需要注册。
⑦ HttpSessionActivationListener:感知自己何时随着HttpSession对象钝化和活化
public void sessionWillPassivate(HttpSessionEvent se) {} : 对象被钝化时调用
public void sessionDidActivate(HttpSessionEvent se) {} : 对象被活化时调用
⑧ HttpSessionBindingListener: 感知自己何时被HttpSession对象绑了(绑在域中)和解绑了。
public void valueBound(HttpSessionBindingEvent event) {} : 对象被绑定时调用
public void valueUnbound(HttpSessionBindingEvent event) {} : 对象被解绑时调用
四 应用: 踢人小案例