《JavaWeb视频教程》(p44)

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>

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值