Listener 监听Session内的对象
Listener用于监控Session内的对象,分别是HttpSessionBindingListener与HttpSessionActivationListener。它们的触发时机分别为:
- HttpSessionBindingListener:当对象被放到Session里时执行valueBound(HttpSessionBindingEvent event)方法。当对象被从Session里移除时执行valueUnbound(HttpSessionBingdingEvent event)方法。对象必须实现该Listener接口。
- HttpSessionActivationListener:服务器关闭时,会将Session里的内容保存到硬盘上,这个过程叫做钝化。服务器重新启动时,会将Session内容从硬盘上重新加载。当Session里的对象被钝化时会执行sessionWillPassivate(HttpSessionEvent event)方法。当对象被重新加载时执行sessionDidActivate(HttpSessionEvent event)。对象必须实现该Listener接口。
事例如下:
package linstener; import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Locale; import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; public class PersonInfo implements HttpSessionActivationListener,HttpSessionBindingListener,Serializable { /**
*
*/
private static final long serialVersionUID = 1L;
private Log log=LogFactory.getLog(this.getClass());
private String name;
private Date dateCreated; @Override
public void sessionDidActivate(HttpSessionEvent arg0) {
//从硬盘恢复后被调用
HttpSession session =arg0.getSession();
log.info(this+"已经成功硬盘中加载。sessionid:" +session.getId());
} @Override
public void sessionWillPassivate(HttpSessionEvent arg0) {
//即将被钝化到硬盘时调用
HttpSession session=arg0.getSession();
log.info(this+"即将保存到硬盘。sessionid:"+session.getId());
} @Override
public void valueBound(HttpSessionBindingEvent arg0) {
//被放入session前调用
HttpSession session=arg0.getSession();
String name=arg0.getName();
log.info(this+"被绑定到session\""+session.getId()+"\"的"+name+"属性上。");
this.setDateCreated(new Date());
} @Override
public void valueUnbound(HttpSessionBindingEvent arg0) {
//从session 中移除后调用
HttpSession session=arg0.getSession();
String name=arg0.getName();
log.info(this+"被从session\""+session.getId()+"\"的"+name+"属性上移除。");
} public Date getDateCreated() {
return dateCreated;
} public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "PersonInfo("+name+")";
} }
这两个Listener监听的是Session中的对象而非Session等,因此不需要在web.xml中声明。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:directive.page import="linstener.PersonInfo" />
<!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>
<%
PersonInfo personInfo =(PersonInfo)session.getAttribute("personInfo");
if(personInfo==null){
personInfo=new PersonInfo();
personInfo.setName("Alex Geng");
session.setAttribute("personInfo", personInfo);
out.println("PersonInfo 对象不存在。已经成功新信件。sessionId:"+session.getId());
}
else{
out.println("PersonInfo对象存在。无需新建。sessionId:"+session.getId());
}
%>
</body>
</html>
Listener 监听Session内的对象的更多相关文章
- Listener 监听对象的属性变化
Listener用于监听Session.context.Request的属性变化,接口名称格式为xxxAttributeListener,包括HttpSessionAttributeListener. ...
- Listener 监听对象的创建和销毁
HttpSessionListener.ServletContextListener.ServletRequestListener分别用于控制Session.context.request的创建和销毁 ...
- java多个listener监听
java 多个listener 监听方法 在class 名称上一行添加@Listeners 括号中用逗号隔开 @Listeners({com.example.MyListener.class,com. ...
- SpringMVC拦截器实现:当用户访问网站资源时,监听session是否过期
SpringMVC拦截器实现:当用户访问网站资源时,监听session是否过期 一.拦截器配置 <mvc:interceptors> <mvc:interceptor> < ...
- 【Java EE 学习 21 上】【其它类型的监听器】【使用HttpSessionActivationListener监听session的活化和钝化】
一.ServletContextListener Method Summary void contextDestroyed(ServletContextEvent sce) R ...
- HttpSessionListener和HttpSessionBindingListener监听session的销毁
1. 使用HttpSessionListener public class OnlineUserListener implements HttpSessionListener { public voi ...
- javaWeb学习之Listener监听
] 一.监听器Listener javaEE包括13门规范 在课程中主要学习 servlet技术 和 jsp技术 其中 servlet规范包括三个技术点:servlet listener filt ...
- Android Listener 监听的几种写法
Android中,View的Listener方法,在是否使用匿名类匿名对象时,有各种不同的写法. OnClickListener和其他Listener方法一样,都是View类的接口,重载实现后就能使用 ...
- 怎样绕过oracle listener 监听的password设置
怎样绕过oracle 监听的password设置: 1.找到监听进程pid ,并将它kill 掉 ps -ef|grep tns [oracle@lixora admin]$ ps -ef|gr ...
随机推荐
- 如何在 Mac 上通过 Boot Camp 安装 Windows?
如何在 Mac 上通过 Boot Camp 安装 Windows? The following contents are chosen from the apple website, thanks f ...
- centOS7 安装man中文手册
[root@localhost ~]# yum list | grep man.*zh -.el7 base [root@localhost ~]# yum -y install man-pages- ...
- php分享二十:mysql优化
1:垂直分割 示例一:在Users表中有一个字段是家庭地址,这个字段是可选字段,相比起,而且你在数据库操作的时候除了个人信息外,你并不需要经常读取或是改写这个字段.那么,为什么不把他放到另外一张表中呢 ...
- Android 启动、绘制、显示过程
Activity 启动过程: startActivity()-> Instrumentation.execStartActivity()-> Binder->ActivityMana ...
- Flink KAFKA
https://data-artisans.com/blog/kafka-flink-a-practical-how-to https://github.com/dataArtisans/kafka- ...
- 每日英语:China Destroys Six Tons of Confiscated Ivory
BEIJING—Chinese government officials destroyed more than six tons of ivory that had been illegally s ...
- 关键词抽取:pagerank,textrank
摘抄自微信公众号:AI学习与实践 TextRank,它利用图模型来提取文章中的关键词.由 Google 著名的网页排序算法 PageRank 改编而来的算法. PageRank PageRank 是一 ...
- 缩放图片,解决bitmap 内存溢出out of memory的问题
很多人在android开发中都遇到了生成bitmap时候内存溢出,也就是out of memory(OOM)的问题,网上对这样的问题的的解决说法不一.笔者作为一个初级开发者,在这里向大家提供一种比较实 ...
- 【ARM】AD转换器
A/D转换器 A/D转换器,又称模/数转换器,顾名思义,就是把模拟信号数字化. 由于系统的实际处理对象往往都是一些模拟量(如温度.压力.位移.图像等),要使计算机或数字仪表能识别和处理这些信号,必须首 ...
- ffmpeg转码参数设置
ffmpeg用了很久了,也没有想写点什么. 刚接触ffmpeg也是有大量的不理解的地方,不过慢慢的了解多了基本上都是可以使用的. 本文主要介绍如何使用ffmpeg.exe进行转码.编译好的ffmpeg ...