session listener的配置和使用
在web.xml中增加listener的配置信息
<listener>
<listener-class>
com.SessionListener(實現session監聽器接口的類的名字,包也要寫上)
</listener-class>
</listener>
<listener>
<listener-class>
com.AnotherSessionListener(有多個session監聽器的時候,加多個<listener>)
</listener-class>
</listener>
在JDK1.5和1.6的javax.servlet.http包中,可以看到session的監聽器接口有4個,
分別是:HttpSessionListener
HttpSessionAttributeListener
HttpSessionBindingListener
HttpSessionActivationListener
要使用這些session的監聽器,必須建立一個實現這些監聽器接口的類,并在web.xml中配置這個類或者在會捆綁到session中的對象中實現監聽器接口。
1. HttpSessionListener :在WEB應用中,當一個session被創建或銷毀時啟用這個監聽器。
HttpSessionListener接口定義了兩個方法:void sessionCreated(HttpSessionEvent se)和void sessionDestroyed(HttpSessionEvent se),每個方法都接受一個HttpSessionEvent對象作為參數。(實現類要提供一個無參數的構造函數)
官方英文說明:Implementations of this interface are notified of changes to the list of active sessions in a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.
package com;
public class SessionListener implements HttpSessionListenter {
public SessionListener() { }; //無參構造方法
//當session被創建時執行這個方法
public void sessionCreated(HttpSessionEvent event) {
//代碼
}
//當session被銷毀時執行這個方法
public void sessionDestoryed(HttpSessionEvent event) {
//代碼
}
.....................................................
}
2. HttpSessionAttributeListener :在當前的WEB應用中,當session的attribute被增加、移除或替換后啟用這個監聽器。
HttpSessionAttributeListener接口定義了三個方法:void attributeAdded(HttpSessionBindingEvent se) 、void attributeRemoved(HttpSessionBindingEvent se) 和 void attributeReplaced(HttpSessionBindingEvent se) ,每個方法都接受一個HttpSessionBindingEvent對象作為參數。
官方英文說明:This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
具體的實現類定義跟上邊的差不多,這里就只寫明接口定義的方法在什么情況下將會執行,后面也將是這樣帶過。
void attributeAdded(HttpSessionBindingEvent se) ; //當一個attribute被增加到session后執行這個方法
void attributeRemoved(HttpSessionBindingEvent se);//當一個attribute被從session中移除后執行這個方法
void attributeReplaced(HttpSessionBindingEvent se);//當一個attribute中的值被替換后執行這個方法
注意上面兩個必須在web.xml中配置, 這是為了比較第三個監聽器,因為你想使用第三個監聽器,那你必須把它布置到準備捆綁到session中的對象,這個對象必須是implements HttpSessionBindingListener
3. HttpSessionBindingListener:當一個實現了該接口的對象被捆綁到session或從session中被解放的時候啟用這個監聽器。(不明白的可以查考 類HttpSessionBindingEvent的說明 和HttpSession.setAttribute方法)
HttpSessionBingdingListener接口定義了兩個方法:void valueBound(HttpSessionBindingEvent event)
和 void valueUnbound(HttpSessionBindingEvent event),每個方法都接受一個HttpSessionBindingEvent對象作為參數。
官方英文說明:Causes an object to be notified when it is bound to or unbound from a session. The object is notified by an HttpSessionBindingEvent object. This may be as a result of a servlet programmer explicitly unbinding an attribute from a session, due to a session being invalidated, or due to a session timing out.
//當對象被捆綁到session中時執行這個方法,像HttpSession.setAttribute("Aname",this_Object);這個方法執行后,將調用下面的方法,并啟用HttpSessionAttributeListener 監聽器
void valueBound( HttpSessionBindingEvent arg2 )
//當對象從session中被解放時執行這個方法,像HttpSession.setAttribute("Aname",this_Object);執行后,再執行HttpSession.setAttribute("Aname",another_Object);方法 或者HttpSession.setAttribute("Aname",“ ”);方法或者removeAttribute("Aname")后,將調用下面的方法,并啟用HttpSessionAttributeListener 監聽器
void valueUnbound( HttpSessionBindingEvent arg1 )
4. HttpSessionActivationListener :用于分布式服務中,當會話被激活時調用相應的事件。
HttpSessionActivationListener接口定義了兩個方法:void sessionWillPassivate(HttpSessionEvent se) 和 void sessionDidActivate(HttpSessionEvent se),每個方法都接受一個HttpSessionEvent對象作為參數。
官方英文說明:Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener. (說實話,這個我看不懂, 當對象所捆綁著的session將被阻塞或激活的時候,捆綁著這個session的對象可能聽從通知它的容器事件。當session在虛擬機之間、或在持久存儲設備中移動時,它就會被阻塞或者激活,容器被要求去通知所有捆綁著session并實現HttpSessionActivationListener接口的attribute對象。我是這樣翻譯的啦,看了的朋友如果覺得有問題,不吝指教)
//當session即將被阻塞時執行這個方法
void sessionWillPassivate(HttpSessionEvent se)
//當session剛被激活時執行這個方法
void sessionDidActivate(HttpSessionEvent se)
下面是HttpSessionEvent類的方法
public HttpSession getSession()
HttpSessionBindingEvent類的方法
public HttpSession getSession()
public java.lang.String getName();
public java.lang.Object getValue()
下面是兩個類都有的通過繼承得到的方法
Methods inherited from class java.util.EventObject |
getSource, toString |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |