jsp或者servlet里面如何得知session和application变量过期的事件?(200分)

  • 主题发起人 主题发起人 bigwater
  • 开始时间 开始时间
B

bigwater

Unregistered / Unconfirmed
GUEST, unregistred user!
不知道可以做到不?
是不是要改写tomcat的配置?
我用的是tomcat和resin
望那位大侠告知
 
HttpSessionBindingListener
 
具体给个例子可以吗???
 
protected voiddo
Get(HttpServletRequest req, HttpServletResponse resp)
{
req.getSession().putValue("expireEventListener",
new HttpSessionBindingListener()
{
void valueBound(HttpSessionBindingEvent event)
{
}
void valueUnbound(HttpSessionBindingEvent event)
{
//when the session expires, an valueUnbound event will be fired.
}
});
}
 
是不是在void valueUNbound(...){里面的语句在事件发生时自动执行的??}
 
valueUnbound事件发生在removeValue的时候。当一个session expire的时候,一般
服务器会remove所有的object。
 
有的网页上有 applet 做的菜单或者小图片什么的,
但是其中更藏有个机关,当网页跳转或者关闭时,会象服务器发出个信息。
虽然与本问题不大相关,但是这个思路是能解决类似的问题:如何确定用户离开。
 
〉当网页跳转或者关闭时,会象服务器发出个信息
想过,没试过。不知道那个事件可以?
 
上面那个判断的代码放在req.getSession().putValue("expireEventListener",
里面,如果session过期,不时就不可以用了啊
我的意思是想像asp那样,实现可以知道session或者application过期的事件
这样好再过期时把数据保存下来,不然随时保存的效率太低了
 
可以让需要退出时保存的对象实现HttpSessionBindingListener
 
多人接受答案了。
 
这么快就结束了?
根据 Servlet API Specification, version 2.3,它定义了 Application Events
Tomcat 4.0 将实现这个功能,以下摘自 4.0 的 README.
http://jakarta.apache.org/cvsweb/index.cgi/jakarta-tomcat-4.0/NEW_SPECS.txt?rev=1.7&content-type=text/vnd.viewcvs-markup
Application Events [Medium Well]
--------------------------------
You can now register event listeners in the web.xml file that receive
notification when the following events occur:
- Application startup
- Application shutdown
- New, replaced, or removed application (servlet context) attributes
- Session created
- Session invalidated or timed out
- New, replaced, or removed session attributes
Application listener functionality has been added to Catalina, and seems to
work fairly well. There are two listeners in the "examples" web application
that simply log things with ServletContext.log() to show that they were reached.
 
呵呵,servlet终于把asp的这招也给学来了。
 
后退
顶部