【JavaEE企业应用实战学习记录】MyGetAttributeListener

本文介绍了一个具体的ServletContextAttributeListener实现——MyServletContextAttributeListener。该监听器能够监控ServletContext范围内的属性变化,包括属性添加、删除和替换,并打印相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 package sanglp.servlet;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.ServletContextAttributeEvent;
 5 import javax.servlet.ServletContextAttributeListener;
 6 import javax.servlet.annotation.WebListener;
 7 
 8 /**
 9  * Created by Administrator on 2016/10/5.
10  * 监听ServletContext范围内属性改变的Listener
11  */
12 @WebListener
13 public class MyServletContextAttributeListener implements ServletContextAttributeListener {
14 
15     @Override
16     public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
17         ServletContext application=servletContextAttributeEvent.getServletContext();
18         //获取添加的属性名和属性值
19         String name=servletContextAttributeEvent.getName();
20         Object value=servletContextAttributeEvent.getValue();
21         System.out.println(application+"范围内添加了"+name+"值为"+value+"的属性");
22 
23 
24     }
25 
26     @Override
27     public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
28         ServletContext application=servletContextAttributeEvent.getServletContext();
29         //获取添加的属性名和属性值
30         String name=servletContextAttributeEvent.getName();
31         Object value=servletContextAttributeEvent.getValue();
32         System.out.println(application+"范围内删除了"+name+"值为"+value+"的属性");
33     }
34 
35 
36     @Override
37     public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
38         ServletContext application=servletContextAttributeEvent.getServletContext();
39         //获取添加的属性名和属性值
40         String name=servletContextAttributeEvent.getName();
41         Object value=servletContextAttributeEvent.getValue();
42         System.out.println(application+"范围内替换了"+name+"值为"+value+"的属性");
43 
44     }
45 }

 

转载于:https://www.cnblogs.com/dream-to-pku/p/5933220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值