由于工作需要,最近在找一些解決方案,發現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,
9
ServletContextAttributeListener
10

{
11
private int count;
12
13
private ServletContext context = null;
14
15
public OnLineCountListener()
16
{
17
count = 0;
18
// setContext();
19
}
20
21
// 創建一個session時激發
22
public void sessionCreated(HttpSessionEvent se)
23
{
24
count++;
25
setContext(se);
26
27
}
28
29
// 當一個session失效時激發
30
public void sessionDestroyed(HttpSessionEvent se)
31
{
32
count--;
33
setContext(se);
34
}
35
36
// 設置context的屬性,它將激發attributeReplaced或attributeAdded方法
37
public void setContext(HttpSessionEvent se)
38
{
39
se.getSession().getServletContext().setAttribute("onLine", new Integer(count));
40
}
41
42
// 增加一個新的屬性時激發
43
public void attributeAdded(ServletContextAttributeEvent event)
44
{
45
46
log("attributeAdded('" + event.getName() + "', '" + event.getValue() + "')");
47
48
}
49
50
// 刪除一個新的屬性時激發
51
public void attributeRemoved(ServletContextAttributeEvent event)
52
{
53
54
log("attributeRemoved('" + event.getName() + "', '" + event.getValue() + "')");
55
56
}
57
58
// 屬性被替代時激發
59
public void attributeReplaced(ServletContextAttributeEvent event)
60
{
61
62
log("attributeReplaced('" + event.getName() + "', '" + event.getValue() + "')");
63
}
64
65
// context刪除時激發
66
public void contextDestroyed(ServletContextEvent event)
67
{
68
69
log("contextDestroyed()");
70
this.context = null;
71
72
}
73
74
// context初始化時激發
75
public void contextInitialized(ServletContextEvent event)
76
{
77
78
this.context = event.getServletContext();
79
log("contextInitialized()");
80
81
}
82
83
private void log(String message)
84
{
85
86
System.out.println("ContextListener: " + message);
87
}
88
}
89
【程序注解】
在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方法,就會自動關閉自己.
而其它的需要改變狀態的對象了是一樣.
下面代碼是曉光兄弟的:
1
package com.kingbole.listener;
2
3
import javax.servlet.http.HttpSessionAttributeListener;
4
import javax.servlet.http.HttpSessionBindingEvent;
5
import javax.servlet.http.HttpSessionEvent;
6
import javax.servlet.http.HttpSessionListener;
7
import org.apache.log4j.Logger;
8
import com.kingbole.context.IContext;
9
import com.kingbole.mn.util.LoginUtil;
10
import com.kingbole.shop.struts.model.User;
11
12
/** *//**
13
*
14
* @author songxg
15
*
16
*/
17
public class KingboleSessionListener implements HttpSessionListener, HttpSessionAttributeListener
18

{
19
20
private static final Logger logger = Logger.getRootLogger();
21
22
/** *//**
23
* 在session被創建時調用,并為其綁定一個初始IContext對象
24
*
25
* @param evt
26
* 事件對象
27
*/
28
public void sessionCreated(HttpSessionEvent evt)
29
{
30
// TODO add your code here:
31
evt.getSession().setAttribute("IContext", new IContext());
32
logger.info("新建Session");
33
}
34
35
/** *//**
36
* 在Session被銷毀時調用,將User的在線狀態更改為下線
37
*
38
* @param evt
39
* 事件對象
40
*/
41
public void sessionDestroyed(HttpSessionEvent evt)
42
{
43
// TODO add your code here:
44
IContext icontext = (IContext) evt.getSession().getAttribute("IContext");
45
if (icontext != null)
46
{
47
User user = icontext.getUser();
48
if (user != null)
49
{
50
int userId = user.getId();
51
if (userId > 0)
52
{
53
// OnlineUsersSet.getInstance().removeUser(userId);
54
logger.info("用戶 " + userId + " 的Session被銷毀");
55
}
56
else
57
{
58
logger.info("錯誤的用戶Session超時");
59
}
60
}
61
else
62
{
63
logger.info("無用戶的Session超時");
64
}
65
}
66
else
67
{
68
logger.info("Session銷毀時IContext上下文為空");
69
}
70
}
71
72
public void attributeAdded(HttpSessionBindingEvent arg0)
73
{
74
// TODO Auto-generated method stub
75
String attribute = arg0.getName();
76
if ("edu.yale.its.tp.cas.client.filter.user".equals(attribute))
77
{
78
IContext icontext = (IContext) arg0.getSession().getAttribute("IContext");
79
if (icontext == null)
80
{
81
icontext = new IContext();
82
}
83
User user = User.loadUserByUserName((String) arg0.getValue());
84
85
icontext.setUser(user);
86
arg0.getSession().setAttribute("IContext", icontext);
87
LoginUtil.mnLogin(attribute, arg0.getSession());
88
}
89
}
90
91
public void attributeRemoved(HttpSessionBindingEvent arg0)
92
{
93
// TODO Auto-generated method stub
94
95
}
96
97
public void attributeReplaced(HttpSessionBindingEvent arg0)
98
{
99
// TODO Auto-generated method stub
100
101
}
102
103
}
104
posted on 2007-07-10 09:49
冰封的愛 閱讀(164)
評論(0) 編輯 收藏 所屬分類:
J2EE