当前位置:网站首页>监听器 Listener
监听器 Listener
2022-07-01 03:10:00 【Al_tair】
监听器 Listener
大家好呀,我是小笙,我和大家分享下我学习Javaweb的笔记
监听器 Listener
概述
Listener 监听器它是 JavaWeb 的三大组件之一,JavaWeb 的三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器
监听器的作用:监听某种变化(一般就是对象创建/销毁, 属性变化), 触发对应方法完成相应的任务
JavaWeb 中有八个监听器, 目前最常用的是 ServletContextListener
ServletContextListener
作用:监听 ServletContext 创建或销毁 (当我们 Web 应用启动时,就会创建 ServletContext)即生命周期监听
应用场景
- 加载初始化的配置文件 比如 spring 的配置文件
- 任务调度(配合定时器 Timer/TimerTask)
方法
void contextInitialized(ServletContextEvent sce) // 创建 Servletcontext 时触发
void contextDestroyed(ServletContextEvent sce) // 销毁 Servletcontext 时触发
代码示例
<?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">
<listener>
<listener-class>com.al_tair.listener.HttpServletContextListener</listener-class>
</listener>
</web-app>
public class HttpServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext 被创建");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext 被销毁");
}
}
ServletContextAttributeListener
作用:监听 ServletContext 属性变化
方法
void attributeAdded(ServletContextAttributeEvent event) // 添加属性
void attributeReplaced(ServletContextAttributeEvent event) // 替换属性
void attributeRemoved(ServletContextAttributeEvent event) // 移除属性
示例代码

// Servlet
@WebServlet(urlPatterns = {
"/listener"})
public class ServletListenerDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet被调用");
ServletContext servletContext = req.getServletContext();
servletContext.setAttribute("name","罗念笙");
servletContext.setAttribute("name","张洛融");
servletContext.removeAttribute("name");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
// listener
public class HttpServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
Object name = servletContextAttributeEvent.getServletContext().getAttribute("name");
if(name != null)
System.out.println("添加属性" + name.toString());
else
System.out.println("添加属性");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("移除属性...");
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("替换属性...");
}
}
HttpSessionListener
作用:监听 Session 创建或销毁,即生命周期监听
方法
void sessionCreated(HttpSessionEvent se) // 创建 session 时调用
void sessionDestroyed(HttpSessionEvent se) // 销毁 session 时调用
HttpSessionAttributeListener
作用:监听 Session 属性的变化
方法
void attributeAdded(ServletRequestAttributeEvent srae) // 添加属性
void attributeReplaced(ServletRequestAttributeEvent srae) // 替换属性
void attributeRemoved(ServletRequestAttributeEvent srae) // 移除属性
ServletRequestListener
作用:监听 Request 创建或销毁,即 Request 生命周期监听,可以用来监控, 某个 IP 访问我们网站的频率, 日志记录 ,访问资源的情况
方法
void requestInitialized(ServletRequestEvent sre) // 创建 request
void requestDestroyed(ServletRequestEvent sre) // 销毁 request
ServletRequestAttributeListener
作用:监听 Request 属性变化
方法
void attributeAdded(ServletRequestAttributeEvent srae) // 添加属性时
void attributeReplaced(ServletRequestAttributeEvent srae) // 替换属性
void attributeRemoved(ServletRequestAttributeEvent srae) // 移除属性
其他监听器
HttpSessionBindingListener 感知监听器
HttpSessionActivationListener 感知监听器
边栏推荐
- JUC学习
- LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
- A few lines of transaction codes cost me 160000 yuan
- Hello World generation
- Stop saying that you can't solve the "cross domain" problem
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
- EtherCAT原理概述
- JUC learning
- 伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
- Scale SVG to container without mask / crop
猜你喜欢

Introduction and installation of Solr

Kmeans

Saving images of different depths in opencv

别再说不会解决 “跨域“ 问题啦

JUC学习
![[applet project development -- JD mall] uni app commodity classification page (first)](/img/6c/5b92fc1f18d58e0fdf6f1896188fcd.png)
[applet project development -- JD mall] uni app commodity classification page (first)

MySQL index --01--- design principle of index

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
![[linear DP] shortest editing distance](/img/2f/9a6f661bf478fdd5d53e5a03d7297d.jpg)
[linear DP] shortest editing distance

Redis tutorial
随机推荐
Redis efficient like and cancel function
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
POI导出excel,按照父子节点进行分级显示
PTA 1017
通信协议——分类及其特征介绍
Hal library operation STM32 serial port
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
Analyze datahub, a new generation metadata platform of 4.7K star
Borrowing constructor inheritance and composite inheritance
调试定位导航遇到的问题总结
Promise中finally的用法
Catch 222222
php批量excel转word
Design practice of current limiting components
Metadata in NFT
JUC学习
mybati sql 语句打印
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
[QT] add knowledge supplement of third-party database
gcc使用、Makefile总结