HttpSessionBindingListener接口
Student类,实现了HttpSessionBindingListener接口;
package com.zzu.listener.vo;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class Student implements HttpSessionBindingListener {//此监听器的作用是监听一个对象添加到session对象的过程
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void valueBound(HttpSessionBindingEvent event) {
System.out.println(this+"Student对象已经添加到session中");
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println(this+"Student对象已经从session中删除");
}
}
准备一个SessionAttributeListener方便观察;
package com.zzu.listener.attribute;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class SessionAttributeListener implements HttpSessionAttributeListener {
public SessionAttributeListener() {
System.out.println("构造方法");
}
@Override
public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("添加");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("删除");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("替换");
}
}
index.jsp
<%@page import="com.zzu.listener.vo.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="./add.jsp">添加</a>
<a href="./delete.jsp">删除</a>
</body>
</html>
add.jsp
<%@page import="com.zzu.listener.vo.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Student student = new Student();
session.setAttribute("student", student);
%>
</body>
</html>
delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
session.removeAttribute("student");
%>
</body>
</html>
启动服务器,访问index页面

点击添加按钮,控制台输出结果为
com.zzu.listener.vo.Student@e4aa01Student对象已经添加到session中
添加
说明执行了执行了valueBound方法;
点击删除按钮,控制台输出结果为
com.zzu.listener.vo.Student@e4aa01Student对象已经从session中删除
删除
说明执行了valueUnbound方法;
博客围绕HttpSessionBindingListener接口展开,介绍了让Student类实现该接口,准备SessionAttributeListener用于观察,还提及index.jsp、add.jsp、delete.jsp等页面。启动服务器访问index页面,点击添加和删除按钮,分别触发valueBound和valueUnbound方法。
1678

被折叠的 条评论
为什么被折叠?



