当前位置:网站首页>监听器 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 感知监听器
边栏推荐
- About the application of MySQL
- 倍福TwinCAT3 Ads相关错误详细列表
- Nacos
- EtherCAT简介
- [linear DP] longest common subsequence
- Huawei operator level router configuration example | configuration static VPLS example
- Catch 222222
- Hal library setting STM32 interrupt
- Lavaweb [first understanding the solution of subsequent problems]
- How to determine the progress bar loaded in the loading interface when opening the game
猜你喜欢
随机推荐
Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
Best used trust automation script (shell)
一文讲解发布者订阅者模式与观察者模式
C#实现图的深度优先遍历--非递归代码
[applet project development -- Jingdong Mall] classified navigation area of uni app
Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
C language EXECL function
php批量excel转word
Ctfshow blasting WP
Subnet division and subnet summary
Is it safe to open an account online in a small securities firm? Will my money be unsafe?
JS日常开发小技巧(持续更新)
Servlet [first introduction]
8 pits of redis distributed lock
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
pytest-fixture
Lavaweb [first understanding the solution of subsequent problems]
js 找出两个数组中的重复元素









