Java登陆第三十一天——监听器

逻辑上与JS中的事件差不多。

被监视的对象触发某些情况,自动执行监听器。

不同于JS的事件,监听器只负责监听三大域对象的相关事件,例如:

  1. 域对象的创建

  2. 域对象的销毁

  3. 域对象数据增删改

八个监听器

Tomcat提供了八个监听器接口作为监听器的规范。

  1. 应用域域监听器

    • ServletContextListener ServletContextAttributeListener
  2. 会话域监听器

    • HttpSessionListener HttpSessionAttributeListener

    • HttpSessionBindingListener HttpSessionActivationListener

  3. 请求域监听器

    • ServletRequestListener ServletRequestAttributeListener

EventListener

所有监听器的父接口。(自定监听器要继承该接口)

监听器

监听器主要分为如下两类:

三大域对象 域对象创建与销毁监听器 域对象数据增删改
ServletContext ServletContextListener ServletContextAttributeListener
HttpSession HttpSessionListener HttpSessionAttributeListener
HttpServletRequest ServletRequestListener ServletRequestAttributeListener

之所以分为两类,是因为每一类监听器的功能都相似。择其一解析。

ServletContextListener

应用域对象创建与销毁的监听器

ServletContextListener 监听ServletContext对象的创建与销毁

public interface ServletContextListener extends EventListener {
    //应用域对象 创建(初始化) 执行的方法
    default void contextInitialized(ServletContextEvent sce) {
    }
    //应用域对象 销毁 执行的方法
    default void contextDestroyed(ServletContextEvent sce) {
    }
}

ServletContextEvent对象

ServletContextEvent主要是为了获取应用域对象。常用方法:

方法名 描述
public ServletContext getServletContext() 获取应用域对象

ServletContextAttributeListener

应用域对象数据增删改的监听器

ServletContextAttributeListener 监听ServletContext中数据的添加、移除和修改

public interface ServletContextAttributeListener extends EventListener {
    //应用域对象 添加 数据执行的方法
    default void attributeAdded(ServletContextAttributeEvent scae) {
    }
    //应用域对象 删除 数据执行的方法
    default void attributeRemoved(ServletContextAttributeEvent scae) {
    }
    //应用域对象 修改 数据执行的方法
    default void attributeReplaced(ServletContextAttributeEvent scae) {
    }
}

ServletContextAttributeEvent对象

ServletContextAttributeEvent主要是为了获取应用域对象中的数据。

同时,ServletContextAttributeEvent继承ServletContextEvent。
常用方法:

方法名 描述
public String getName() 获取数据名
public Object getValue() 获取数据值

在web.xml中配置监听器

   <listener>
       <listener-class>监听器全类名</listener-class>
   </listener>

使用注解配置监听器

单纯 @WebListener 即可

@WebListener
public class Test8scListener implements ServletContextListener{}

应用域对象创建与销毁的监听器栗子

Test8scListener

@WebListener
public class Test8scListener implements ServletContextListener{
    //应用域对象 创建(初始化) 执行的方法
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        System.out.println(context.hashCode()+"应用域对象初始化");
    }
    //应用域对象 销毁 执行的方法
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        System.out.println(context.hashCode()+"应用域对象销毁");
    }
}

启动并关闭一次项目,并查看控制台

//控制台输出,过滤了某些信息
1288035353应用域对象初始化
1288035353应用域对象销毁

应用域对象域对象数据增删改的监听器栗子

Test8scListener

@WebListener
public class Test8scListener implements ServletContextListener, ServletContextAttributeListener {
    //应用域对象 添加 数据执行的方法
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        ServletContext context = scae.getServletContext();
        System.out.println(context.hashCode()+"应用域添加了数据"+scae.getName()+":"+scae.getValue());
    }
    //应用域对象 删除 数据执行的方法
    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        ServletContext context = scae.getServletContext();
        System.out.println(context.hashCode()+"应用域删除了数据"+scae.getName()+":"+scae.getValue());
    }
    //应用域对象 修改 数据执行的方法
    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        ServletContext context = scae.getServletContext();
        Object oldValue = scae.getValue();//获取老数据
        Object newValue = context.getAttribute(scae.getName());//获取最新数据
        System.out.println(context.hashCode()+"应用域修改了数据"+scae.getName()+":"+oldValue+
        "为"+scae.getName()+":"+newValue);
    }
}

TestServlet8,新增数据

@WebServlet("/Test8add")
public class TestServlet8 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
        context.setAttribute("temp","2023拜拜");
    }
}

TestServlet8B,修改数据

@WebServlet("/Test8replace")
public class TestServlet8B extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
        context.setAttribute("temp","2024快乐");
    }
}

TestServlet8C,删除数据

@WebServlet("/Test8remove")
public class TestServlet8C extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
        context.removeAttribute("temp");
    }
}

客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test8add

//控制台输出
476940799应用域添加了数据temp:2023拜拜

客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test8replace

//控制台输出
476940799应用域修改了数据temp:2023拜拜为temp:2024快乐

客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test8remove

//控制台输出
476940799应用域删除了数据temp:2024快乐

特殊的监听器

会话域还有两个特殊的监听器这里不做介绍

  • HttpSessionBindingListener

  • HttpSessionActivationListener

posted @   rowbed  阅读(19)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示