监听器Listener学习笔记

2017-11-29 19:28:45

    1.Listener概述

    什么是监听器?

    在Java Web中监听器指的是对这个web环境的监听,当被监听的对象发生变化时,可以执行相应的方法进行处理.

    监听器在开发中常见的场景:手机编程,前端编程

    监听器的相关概念:

    事件源:被监听的对象

    监听器:用于监听事件源的对象

    注册监听器:将监听器与事件源进行绑定

    响应行为:监听器监听到事件源状态变化时,所涉及的功能代码

    2.监听器的分类

    监听器有哪些?

        第一类:按照被监听的对象划分:ServletContext域,ServletRequest域,HttpSession域

        第二类:监听的内容划分:监听域对象的创建与销毁的.监听域对象的属性变化的.

   

    域对象的创建与销毁

     1.ServletContextListener,HttpSessionListener,ServletRequestListener

     域对象内的属性的变化

    ServletContentAttributeListener,HttpSessionAttributeListener,ServletRequestAttributeListener  

    额外两个监听器:

    HttpSessionActivationListener

    HttpSessionBindingListener

    下面是一个简单的监听器实现

package net.zixue.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener()
public class FirstServletContextListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public FirstServletContextListener() {
    }

    // -------------------------------------------------------
    // 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("servletContext初始化了");
    }

    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("servletContext销毁了");
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
      /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
      /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}

    运行程序,通过控制台可以看到

    1.png

    监听器成功执行了

    3.HttpSessionListener

    监听Httpsession域的创建于销毁的监听器HttpSessionListener

    HttpSession对象的生命周期

    创建:第一次调用request.getSession时创建

    销毁:服务器关闭销毁,session过期或手动销毁

    4.ServletRequestListener

    监听ServletRequest域创建与销毁的监听器ServletRequestListener

    ServletRequest的生命周期

    创建:每一次请求都会创建request

    销毁:请求结束

    5.域对象属性监听器

    ServletContextAttributeListener监听器

    HttpSessionAttributeListener监听器

    ServletRequestAttributeListener监听器

    监听三大域对象属性的变化

    域对象的通用方法:

    setAttribute(name,value)

        触发添加属性的监听器的方法

        触发修改属性的监听器的方法

    getAttribute(name)

        查询操作不会触发监听器

    removeAttribute(name)

        触发删除属性的监听器的方法

    6.对象感知监听器

    与session中的绑定的对象相关的监听器(对象感知监听器)

    HttpSesssionBindingListener

    绑定状态:就是一个对象被放到session域中 setAttribute()

    解绑状态:就是这个对象从session域中移除了 removeAttribute()

    HttpSessionActivationListener

    钝化状态:是将session内存中的对象持久化(序列化)到磁盘

    活化状态:就是将磁盘上的对象再次恢复到session内存中