p44 session 绑定解绑,钝化活化
BeanListener2.java
package org.student.listener;
import java.io.Serializable;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
//监听BeanListener2对象的钝化,活化
public class BeanListener2 implements HttpSessionActivationListener, Serializable{
//准备被钝化,活化的数据
private int num;
private String user;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
//监听时刻:即将钝化之前
@Override
public void sessionWillPassivate(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
System.out.print("即将钝化之前: BeanListener2的对象,会随着session的钝化而钝化");
}
//监听时刻:刚刚进行了活化之后
@Override
public void sessionDidActivate(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
System.out.print("刚刚进行了活化之后: BeanListener2的对象,会随着session的活化而活化");
}
}
write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.student.listener.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
BeanListener2 bean = new BeanListener2();
bean.setNum(10);
bean.setUser("zs");
session.setAttribute("bean", bean);
%>
</body>
</html>
结果:
关闭服务器,重新打开后,进入read.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
从硬盘中读取session域中的对象(活化):<br/>
num:${sessionScope.bean.num}<br/>
user:${sessionScope.bean.user}<br/>
</body>
</html>