bean实现了HttpSessionBindingListener, HttpSessionActivationListener可以监听session绑定对象,解除对象时的事件。active如果不活动则序列化其中的bean到缓冲区,需要实现序列化接口。
--------------------------------servlet的操作------------------------------------
System.out.println(session.getId());
session.setAttribute("a", new serializableBean());
--------------------------------server.xml中host的配置------------------------------------
对path的请求,对应{catalina.home}webapps/Life_的目录,下面的fileStore是directory,当session passivate时序列化value(bean)到该目录下。didactivate时反序列化回来。此文件夹不存在则创建,反序列化时把原序列化的文件delete,命名为session的id.session.反序列化时生产新的sessionid,注意这和session的发呆时间设置有关。
<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
<Store className="org.apache.catalina.session.FileStore" directory="d:/lifstore">
</Store>
</Manager>
---------------------------bean --------------------------------------------
package com.aowin.life;
import java.io.Serializable;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
public class serializableBean implements HttpSessionBindingListener,
HttpSessionActivationListener,Serializable{
private static final long serialVersionUID = 646677814484353386L;
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("value bound");
}
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("value unbound");
}
public void sessionDidActivate(HttpSessionEvent se) {
System.out.println("session didactivate ");
}
public void sessionWillPassivate(HttpSessionEvent se) {
System.out.println("will passivate ");
}
}