由于工作需要,最近在找一些解决方案,发现Listener是一个很好的东西,
能够监听到session,application的create,destroy,可以监听到session,application
属性绑定的变化,考虑了一下,可以应用在"在线人数统计","数据缓存"等各个方面,
下面是整理的一些资料.
Listener 是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个 HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:
ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。
ServletContextListener 监听ServletContext。当创建ServletContext时,激发contextInitialized (ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener 监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener 监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded (HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
下面我们开发一个具体的例子,这个监听器能够统计在线的人数。在ServletContext初始化和销毁时,在服务器控制台打印对应的信息。当ServletContext里的属性增加、改变、删除时,在服务器控制台打印对应的信息。
要获得以上的功能,监听器必须实现以下3个接口:
HttpSessionListener
ServletContextListener
ServletContextAttributeListener
我们看具体的代码,见示例14-9。
【程序源代码】
1 // ==================== Program Discription =====================
2 // 程序名称:示例14-9 : EncodingFilter .java
3 // 程序目的:学习使用监听器
4 // ==============================================================
5 import javax.servlet.http.*;
6 import javax.servlet.*;
7
8 public class OnLineCountListener implements HttpSessionListener,
ServletContextListener,ServletContextAttributeListener
9 {
10 private int count;
11 private ServletContext context = null;
12
13 public OnLineCountListener()
14 {
15 count=0;
16 //setContext();
17 }
18 //创建一个session时激发
19 public void sessionCreated(HttpSessionEvent se)
20 {
21 count++;
22 setContext(se);
23
24 }
25 //当一个session失效时激发
26 public void sessionDestroyed(HttpSessionEvent se)
27 {
28 count--;
29 setContext(se);
30 }
31 //设置context的属性,它将激发attributeReplaced或attributeAdded方法
32 public void setContext(HttpSessionEvent se)
33 {
34 se.getSession().getServletContext().
setAttribute("onLine",new Integer(count));
35 }
36 //增加一个新的属性时激发
37 public void attributeAdded(ServletContextAttributeEvent event) {
38
39 log("attributeAdded('" + event.getName() + "', '" +
40 event.getValue() + "')");
41
42 }
43
44 //删除一个新的属性时激发
45 public void attributeRemoved(ServletContextAttributeEvent event) {
46
47 log("attributeRemoved('" + event.getName() + "', '" +
48 event.getValue() + "')");
49
50 }
51
52 //属性被替代时激发
53 public void attributeReplaced(ServletContextAttributeEvent event) {
54
55 log("attributeReplaced('" + event.getName() + "', '" +
56 event.getValue() + "')");
57 }
58 //context删除时激发
59 public void contextDestroyed(ServletContextEvent event) {
60
61 log("contextDestroyed()");
62 this.context = null;
63
64 }
65
66 //context初始化时激发
67 public void contextInitialized(ServletContextEvent event) {
68
69 this.context = event.getServletContext();
70 log("contextInitialized()");
71
72 }
73 private void log(String message) {
74
75 System.out.println("ContextListener: " + message);
76 }
77 }
【程序注解】
在OnLineCountListener 里,用count代表当前在线的人数,OnLineCountListener将在Web服务器启动时自动执行。当 OnLineCountListener构造好后,把count设置为0。每增加一个Session,OnLineCountListener会自动调用 sessionCreated(HttpSessionEvent se)方法;每销毁一个Session,OnLineCountListener会自动调用sessionDestroyed (HttpSessionEvent se)方法。当调用sessionCreated(HttpSessionEvent se)方法时,说明又有一个客户在请求,此时使在线的人数(count)加1,并且把count写到ServletContext中。 ServletContext的信息是所有客户端共享的,这样,每个客户端都可以读取到当前在线的人数。
从作用域范围来说,Servlet的作用域有ServletContext,HttpSession,ServletRequest.
Context范围:
ServletContextListener:
对一个应用进行全局监听.随应用启动而启动,随应用消失而消失主要有两个方法:
contextDestroyed(ServletContextEvent event)
在应用关闭的时候调用
contextInitialized(ServletContextEvent event)
在应用启动的时候调用
这个监听器主要用于一些随着应用启动而要完成的工作,也就是很多人说的我想在容器
启动的时候干..........
一般来说对"全局变量"初始化,如
public void contextInitialized(ServletContextEvent event){
ServletContex sc = event.getServletContext();
sc.setAttribute(name,value);
}
以后你就可以在任何servlet中getServletContext().getAttribute(name);
我最喜欢用它来做守护性工作,就是在contextInitialized(ServletContextEvent event)
方法中实现一个Timer,然后就让应用在每次启动的时候让这个Timer工作:
程序代码:
public void contextInitialized(ServletContextEvent event){
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
//do any things
}
},0,时间间隔);
}
有人说Timer只能规定从现在开始的多长时间后,每隔多久做一次事或在什么时间做
一次事,那我想在每月1号或每天12点做一项工作如何做呢?
你只要设一个间隔,然后每次判断一下当时是不是那个时间段就行了啊,比如每月一号做,那你
时间间隔设为天,即24小时一个循环,然后在run方法中判断当时日期new Date().getDate()==1
就行了啊.如果是每天的12点,那你时间间隔设为小时,然后在run中判断new Date().getHour()
==12,再做某事就行了.
ServletContextAttributeListener:
这个监听器主要监听ServletContex对象在setAttribute()和removeAttribute()的事件,注意
也就是一个"全局变量"在被Add(第一次set),replace(对已有的变量重新赋值)和remove的时候.
分别调用下面三个方法:
public void attributeAdded(ServletContextAttributeEvent scab)这个方法不仅可以知道
哪些全局变量被加进来,而且可获取容器在启动时自动设置了哪些context变量:
程序代码:
public void attributeAdded(ServletContextAttributeEvent scab){
System.out.println(scab.getName());
}
public void attributeRemoved(ServletContextAttributeEvent scab)
public void attributeReplaced(ServletContextAttributeEvent scab)
Session范围:
HttpSessionListener:
这个监听器主要监听一个Session对象被生成和销毁时发生的事件.对应有两个方法:
程序代码:
public void sessionCreated(HttpSessionEvent se)
public void sessionDestroyed(HttpSessionEvent se)
一般来说,一个session对象被create时,可以说明有一个新客端进入.可以用来粗略统计在线人
数,注意这不是精确的,因为这个客户端可能立即就关闭了,但sessionDestroyed方法却会按一定
的策略很久以后才会发生.
HttpSessionAttributeListener:
和ServletContextAttributeListener一样,它监听一个session对象的Attribut被Add(一个特定
名称的Attribute每一次被设置),replace(已有名称的Attribute的值被重设)和remove时的事件.
对就的有三个方法.
程序代码:
public void attributeAdded(HttpSessionBindingEvent se)
public void attributeRemoved(HttpSessionBindingEvent se)
public void attributeReplaced(HttpSessionBindingEvent se)
上面的几个监听器的方法,都是在监听应用逻辑中servlet逻辑中发生了什么事,一般的来说.
我们只要完成逻辑功能,比如session.setAttribute("aaa","111");我只要把一个名为aaa的变量
放在session中以便以后我能获取它,我并不关心当session.setAttribute("aaa","111");发生时
我还要干什么.(当然有些时候要利用的),但对于下面这个监听器,你应该好好发解一下:
HttpSessionBindingListener:
上面的监听器都是作为一个独立的Listener在容器中控制事件的.而HttpSessionBindingListener
对在一对象中监听该对象的状态,实现了该接口的对象如果被作为value被add到一个session中或从
session中remove,它就会知道自己已经作为一个session对象或已经从session删除,这对于一些非
纯JAVA对象,生命周期长于session的对象,以及其它需要释放资源或改变状态的对象非常重要.
比如:
session.setAttribute("abcd","1111");
以后session.removeAttribute("abcd");因为abcd是一个字符中,你从session中remove后,它就会
自动被垃圾回收器回收,而如果是一个connection:(只是举例,你千万不要加connection往session
中加入)
程序代码:
session.setAttribute("abcd",conn);
以后session.removeAttribute("abcd");这时这个conn被从session中remove了,你已经无法获取它
的句柄,所以你根本没法关闭它.而在没有remove之前你根本不知道什么时候要被remove,你又无法
close(),那么这个connection对象就死了.另外还有一些对象可以在被加入一个session时要锁定
还要被remove时要解锁,应因你在程序中无法判断什么时候被remove(),add还好操作,我可以先加锁
再add,但remove就后你就找不到它的句柄了,根本没法解锁,所以这些操作只能在对象自身中实现.
也就是在对象被add时或remove时通知对象自己回调相应的方法:
程序代码:
MyConn extends Connection implements HttpSessionBindingListener{
public void valueBound(HttpSessionBindingEvent se){
this.initXXX();
}
public void valueUnbound(HttpSessionBindingEvent se){
this.close();
}
}
session.setAttribute("aaa",new MyConn());
这时如果调用session.removeAttribute("aaa"),则触发valueUnbound方法,就会自动关闭自己.
而其它的需要改变状态的对象了是一样.
相关推荐
简单的例子,通过TcpClient TcpListener 实现Socket 编程,
我们在编写JSP/Servle程序时,有相应的事件处理机制,在JSP/Servlet中是在web.xml中注册Listener,由Container在特定事件发生时呼叫特定的实现Listener的类。
C#,winform,Tcp通信源码 使用TcpListener和TcpClient 源码 Tcp通信源码TcpListener和TcpClient 源码
event_listener.jsx PS事件监听脚本
NULL 博文链接:https://crazyhacker.iteye.com/blog/662587
11.1 Web应用的事件模型 11.2 Web应用的过滤器 11.3 小 结 11.1.1 事件与监听器接口 11.1.2 监听环境事件 11.1.3 监听请求事件 11.1.4 监听会话事件 11.1.5 事件监听器的注册
· Listener的应用实例 利用HttpSessionListener统计多在线用户人数 Spring使用ContextLoaderListener加载ApplicationContext配置信息 Spring使用Log4jConfigListener配置Log4j日志 Spring使用...
React Native Android Notification Listener是一个库,可让您侦听来自所有应用程序的状态栏通知。 (仅限Android) 安装 $ yarn add react-native-android-notification-listener 或者 $ npm install react-...
TcpClient类和TcpListener类属于.NET框架下网络通信中的应用层类,为Socket通信提供了更简单,对用户更为友好的接口。应用层类比位于底层的Socket类提供了更高层次的抽象,封装了套接字的创建,不需要处理连接的细节...
2.1.3 TcpClient与TcpListener类 2.1.4 解决TCP的无消息边界问题 2.2 利用同步TCP编写网络游戏 2.2.1 服务器端编程 2.2.2 客户端编程 2.3 异步TCP应用编程 2.3.1 EventWaitHandle类 ...
我们在处理数据库的问题的时候,有时在数据库端看不到异常的等待,但是应用却报连接异常。 这种情况,有可能是在压力还没有传递到数据库,在网络这层已经发生问题了。如连接风暴,此时的数据库listener的进程已经忙...
简易画图软件。...库来实现基本图形的绘制和应用,以及对于颜色的设置和对于component 的移动和大小改 变。其中使用了java 的对于鼠标的监控功能来获得鼠标的点击,释放以及拖拽等不同的 listener 响应。
应用最新bundle patch后,扫描依然报出漏洞 Oracle Database Server ‘TNS Listener’远程数据投毒漏洞(CVE-2012-1675) •1.确定解决方案 •2.应用解决方案 •3.验证修补情况 •4.Reference 1.确定解决方案 ...
1、Filter的理解和应用 实现一个禁止缓存的过滤器。 要求和提示: (1)禁止浏览器缓存所有...3、Listener的理解和应用 通过监听器记录在线用户的姓名,在页面进行用户姓名的显示,同时实现对某个用户的强制下线功能。
9.2.2 实现MessageListener 352 9.2.3 MDB的生命周期 353 9.2.4 MDB中的依赖注入 356 9.2.5 事务管理和异常处理 359 9.3 使用NetBeans开发EJB 359 9.3.1 使用NetBeans开发Session Bean 359 9.3.2 使用NetBeans开发...
语言:español 收听Nfc串行监视器消息 该应用程序监听NFC读取器设备,并将事件发送到DispatchApp。
如果您的整个应用程序都在React内部,那么这很好,但如果将React组件插入包含其他事件侦听器的页面中,效果就不那么理想了。 如果事件在您的React组件内部发生,则您的组件将是最后一个听到该事件的组件。 该事件将...
侦听器规则扩展该模块提供了,可通过侦听器规则将您的服务注册到应用程序负载均衡器。 默认情况下,通过此扩展名添加的规则在负载均衡器中自动进行优先级排序。 自动优先级从一个数字(默认值:1)开始,并针对没有...
VICREO侦听器Windows,Mac和Linux的热键侦听器转到进行下载。... 该应用程序首先查找名为type的键。 可以使用以下类型; 按下(模拟按键) 文件(打开文件) shell(执行shell命令) 按下(模拟按键按
在我们android开发中,一个应用使用的内存大小是有限制的.在应用中,如果大量的使用bitmap就很可能导致内存溢出的问题。比如我在曾经的一个项目中遇到的问题:要使用Gallery来显示多张不同的图片,在给Gallery的每个...