与session中绑定的对象相关。
1、session的绑定与解绑:
(1)创建Person类,实现HttpSessionBindingListener 接口,用来监听session对象的绑定与解绑。
package pers.zhb.domain;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class Student implements HttpSessionBindingListener {
private String studentno;
private String sname;
private String sex;
@Override
public String toString() {
return "Student{" +
"studentno='" + studentno + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
'}';
}
public String getStudentno() {
return studentno;
}
public void setStudentno(String studentno) {
this.studentno = studentno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("Student对象被绑定了。");
}
@Override
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("Student对象被解绑了");
}
}
(2)创建Servlet,向Session中添加对象元素,并将对象元素移除:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student student=new Student();
student.setSex("男");
student.setSname("zhai");
student.setStudentno("20171514141");
HttpSession session=request.getSession();
session.setAttribute("student",student);
session.removeAttribute("student");
}

与域对象监听器不同,对象感知监听器不需要在web.xml文件中进行配置。
2、session对象的钝化与活化:
钝化:将Session中的对象持久化存储到磁盘上。
活化:将磁盘上的对象再次恢复到Session内存中。
(1)钝化:在Student类中实现HttpSessionActivationListener接口,用来监听Session中的对象的钝化与活化。
package pers.zhb.domain;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
import java.io.Serializable;
public class Student implements HttpSessionActivationListener , Serializable {
private String studentno;
private String sname;
private String sex;
@Override
public String toString() {
return "Student{" +
"studentno='" + studentno + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
'}';
}
public String getStudentno() {
return studentno;
}
public void setStudentno(String studentno) {
this.studentno = studentno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
System.out.println("session域中的对象被钝化了");
}
@Override
public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
System.out.println("Session域中的对象被活化了");
}
}
(2)创建一个Servlet将Student对象存放到Session域中:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student student=new Student();
student.setSex("男");
student.setSname("zhai");
student.setStudentno("20171514141");
HttpSession session=request.getSession();
session.setAttribute("student",student);
System.out.println("Student对象已经被放到Session域当中了");
}
(3)钝化过程:先访问Servlet,将Student对象存放到Session中,关闭服务器的时候被钝化。可以在服务器work文件夹下面查看。(.ser文件)
(4)将不经常用的Session对象钝化到磁盘上:

在META-INF目录下,创建context.xml文件,可以通过设置时间将Session中存储的对象自动钝化:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- maxIdleSwap:session中的对象多长时间不使用就钝化 -->
<!-- directory:钝化后的对象的文件写到磁盘的哪个目录下 配置钝化的对象文件在 work/catalina/localhost/钝化文件 -->
<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
<Store className="org.apache.catalina.session.FileStore" directory="dunhua" />
</Manager>
</Context>
