当前位置: 首页 > news >正文

11_JavaWeb监听器

文章目录

  • 监听器
  • 1.监听器的分类
  • 2.application域监听器案例

监听器

  • 概念:后端要发生一些事情的时候,自动触发一些代码的执行;

在这里插入图片描述

1.监听器的分类

  • web中定义八个监听器接口作为监听器的规范,这八个接口按照不同的标准可以形成不同的分类

  • 按监听的对象划分

    • application域监听器 ServletContextListener ServletContextAttributeListener
    • session域监听器 HttpSessionListener HttpSessionAttributeListener HttpSessionBindingListener HttpSessionActivationListener
    • request域监听器 ServletRequestListener ServletRequestAttributeListener
  • 按监听的事件分

    • 域对象的创建和销毁监听器 ServletContextListener HttpSessionListener ServletRequestListener
    • 域对象数据增删改事件监听器 ServletContextAttributeListener HttpSessionAttributeListener ServletRequestAttributeListener
    • 其他监听器 HttpSessionBindingListener HttpSessionActivationListener

2.application域监听器案例

注:本章节仅讲述应用域监听器接口
案例
设计三个Servlet分别进行增加数据,修改数据,删除数据
创建隔壁包下进行监听
实现ServletContextListener,实现其方法
配置映射
xml文件配置listener 或者直接添加注解即可
  • 构建Servlet1
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = this.getServletContext();servletContext.setAttribute("keya","valuea");}
}
  • 监听Servlet1的构建以及销毁

    • 应用域监听接口以及api

    • 接口是HttpServletContextListener

    • 实现方法是contextInitialized、contextDestroyed

在这里插入图片描述

  • 监听数据 增删改–多实现一个方法ServletContextAttributeListener
@WebListener("/Servlet1")
public class MyApplicationListener implements ServletContextListener, ServletContextAttributeListener {@Overridepublic void attributeAdded(ServletContextAttributeEvent scae) {ServletContextAttributeListener.super.attributeAdded(scae);}@Overridepublic void attributeRemoved(ServletContextAttributeEvent scae) {ServletContextAttributeListener.super.attributeRemoved(scae);}@Overridepublic void attributeReplaced(ServletContextAttributeEvent scae) {ServletContextAttributeListener.super.attributeReplaced(scae);}
}
attributeAdded()
attributeRemoved()
attributeReplaced()
  • 监听接口中可以重写相关方法,一旦被监听的对象执行了某项操作,tomcat后台就会进行响应,监听到;

  • 例如attributeAdded就监听this.getContext().setAttribute()这个方法的操作

  • 一般来说监听的东西五花八门,用的时候查即可;

http://www.lryc.cn/news/362939.html

相关文章:

  • jmeter常用的断言
  • Opencv Python图像处理笔记二:图像变换、卷积、形态学变换
  • 使用若依框架RuoYi前后端分离版+运行+自动生成页面进行导入进行开发+工具(完整版)
  • 开源博客项目Blog .NET Core源码学习(29:App.Hosting项目结构分析-17)
  • 2021 hnust 湖科大 计组课设 包含multisim14连线文件,报告,指导书
  • CHATGPT升级plus(已有账号前提下)
  • 通过血清拉曼光谱进行COVID-19的高效初步筛查
  • Java 性能调优与监控工具详解
  • LeetCode 1633, 122, 239
  • (十五)统计学基础练习题九(选择题T401-450)
  • 用大白话讲解下 CNN和卷积核( 百度 AI 回答 )
  • 安全生产新篇章:可燃气体报警器检验周期的国家标准解读
  • github搭建个人博客
  • Linux系统之mv命令的基本使用
  • vscode 好用的插件
  • 什么是蓝牙定位车载GPS终端
  • 指纹采集技术
  • /etc/fstab、/etc/mtab 文件详解及永久挂载(文件系统、ISO镜像、文件网络共享)
  • 【Linux】进程(5):命令行参数
  • vue2+antv/x6实现er图
  • 如何在XDMA中查看LTSSM状态机
  • 编译和运行qemu-uboot-arm64单板的Armbian系统
  • Python版《消消乐》,附源码
  • Kubernetes ingress
  • 【JavaScript】ECMAS6(ES6)新特性概览(二):解构赋值、扩展与收集、class类全面解析
  • Linux入门学习指南
  • 纯血鸿蒙实战开发—如何添加顶部tab页面
  • 数仓建模—指标拆解和选取
  • HTML静态网页成品作业(HTML+CSS)——VIVO介绍网页(1个页面)
  • MySQL(四) - SQL优化