sesion的钝化和活化

钝化:内存->硬盘
活化:硬盘->内存

session对象的四种状态
监听绑定和解绑:HttpSessionBindingListener 不需要配置web.xml
a.session.setAttribute("a",xxx) 将对象a【绑定】到session中
b.session.removeAttribute("a") 将对象a从session中【解绑】

public void valueBound(HttpSessionBindingEvent event) {
System.out.println("绑定Bean对象(将Bean对象增加到session域中),绑定的对象:"+this+",SessionId:"+event.getSession().getId());
}
//session.setAttribute("a",...)
其中this代表了绑定的对象:"a"
event:可以获取存储a对象的session
第一次访问:
绑定Bean对象:org.student.listener.BeanListener@173b92e7
SessionId:79CF97BBBE36B3C025DC91BDBDF82349
刷新
绑定Bean对象:org.student.listener.BeanListener@4f76fe51
SessionId:79CF97BBBE36B3C025DC91BDBDF82349
解绑Bean对象:org.student.listener.BeanListener@173b92e7
SessionId:79CF97BBBE36B3C025DC91BDBDF82349

监听session对象的钝化、活化:HttpSessionActivationListener 不需要配置web.xml
c.钝化
d.活化

如何钝化,活化:配置tomcat安装目录/conf/context.xml
<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="5">
<Store className="org.apache.catalina.session.FileStore" directory="myy"/>
</Manager>

钝化,活化本质 就是序列化、反序列化:序列化、反序列化需要Serializable

总结:钝化、活化实际执行是通过context.xml中进行配置而进行
活化:session中获取某一个对象时,如果该对象不存在,则直接尝试 从之前钝化的文件中去获取(活化)
HttpSessionActivationListener只是复制在session钝化和活化时予以监听。
需要实现Serializable

原文地址:https://www.cnblogs.com/mayouyou/p/13131486.html