Session监听器

session listener的配置和使用java 2009-12-04 10:54:51 阅读39 评论0 字号:大中小
在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值