一、监听三个域对象创建和销毁的事件监听器
1. ServletContextListener接口
public class MyServletContextListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println(arg0.getServletContext() + "被创建了");
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println(arg0.getServletContext() + "被销毁了");
}
}
在web.xml文件中配置
<listener> <listener-class>cn.class3g.web.listener.MyServletContextListener
</listener-class>
</listener>
执行结果为:启动服务器时打印”被创建了“停止服务器时打印“被销毁了”;
2.HttpSessionListener接口
创建类HttpSessionListener实现HttpSessionListenerpublic class MyHttpSessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println(arg0.getSource() + "被销毁了");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println(arg0.getSource() + "被创建了");
}
}
在web.xml文件中配置
<listener>
<listener-class>cn.csdn.web.listener.MyHttpSessionListener</listener-class>
</listener>
执行结果为:访问index.jsp时被创建了而且访问index.html不会被访问,因为如jsp页面中的page命令中有session="true"如果设置为"false"时就不会被创建了;
当刷新jsp页面时会再次创建再次打印;关闭浏览器时不会销毁,因为session默认存在30分钟;
HttpSessionListener的作用:统计在线人数
3. ServletRequestListener接口
创建类MyServletRequestListener实现ServletRequestListener
public class MyServletRequestListener implements ServletRequestListener {
public void requestDestroyed(ServletRequestEvent arg0) {
System.out.println("请求对象"+arg0.getSource() + "被销毁了");
}
public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("请求对象"+arg0.getSource() + "被创建了");
}
}
在web.xml文件中配置
<listener>
<listener-class>cn.csdn.web.listener.MyServletRequestListener</listener-class>
</listener>
执行结果为:当访问idex.jsp时被创建随后被销毁了;
ServletRequestListener的实际用途:统计网站的请求数量,可以计算网站性能(实际应用不多)
1. ServletContextAttributeListener接口
创建类MyServletContextAttributeListener实现ServletContextAttributeListener
public class MyServletContextAttributeListener implements
ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent arg0) {
ServletContext context = arg0.getServletContext();
Object data = arg0.getValue();
System.out.println(context + "中加入了"+data);
}
public void attributeRemoved(ServletContextAttributeEvent arg0) {
ServletContext context = arg0.getServletContext();
Object data = arg0.getValue();
System.out.println(context + "中删除了"+data);
}
public void attributeReplaced(ServletContextAttributeEvent arg0) {
ServletContext context = arg0.getServletContext();
Object data = arg0.getValue();
System.out.println(context + "中更新了"+data);
}
}
在web.xml文件中配置
<listener>
<listener-class>cn.csdn.web.listener.MyservletContextAttributeListener</listener-class>
</listener>
index.jsp中写到:
<%
application.setAttribute("param","xxx");
application.setAttribute("param","yyy");
application.removeAttribute("param");
%>
执行结果:
2. HttpSessionAttributeListener接口和ServletRequestAttributeListener接口
创建MyHttpSessionAndRequestAttributeListener类分别实现上面两个接口public class MyHttpSessionAndRequestAttributeListener implements
HttpSessionAttributeListener, ServletRequestAttributeListener {
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("session中增加了属性");
}
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("session中删除了属性");
}
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("session中属性被替换了");
}
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("request中增加了属性");
}
public void attributeRemoved(ServletRequestAttributeEvent srae) {
System.out.println("request中删除了属性");
}
public void attributeReplaced(ServletRequestAttributeEvent srae) {
System.out.println("request中属性被替换了");
}
}
web配置:
<listener>
<listener-class>cn.csdn.web.listener.MyHttpSessionAndRequestAttributeListener</listener-class>
</listener>
index.jsp 写到:
<%
session.setAttribute("param","aaa");
session.setAttribute("param","bbb");
session.removeAttribute("param");
request.setAttribute("param","aaa");
request.setAttribute("param","bbb");
request.removeAttribute("param");
%>
执行结果:
request中增加了属性
session中增加了属性
session中属性被替换了
session中删除了属性
request中增加了属性
request中属性被替换了
request中删除了属性
三、感知 Session 绑定的事件监听器
1. HttpSessionBindingListener接口
实现了HttpSessionBindingListener接口的 JavaBean 对象可以感知自己被绑定到 Session 中和从 Session 中删除的事件
创建user实现HttpSessionBindingListener
public class User implements HttpSessionBindingListener{
private String name;
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("到session中去");
}
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("从session中移除");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
index.jsp中写:
<%
session.setAttribute("user",new User());
session.removeAttribute("param");
%>
不用配置web文件
执行结果:一访问网页
到session中去
从session中移除
实际用途:
统计网站在线用户(和在线人数不同),当前网站在线用户(作业)
2. HttpSessionActivationListener接口
实现了HttpSessionActivationListener接口的 JavaBean 对象可以感知自己被活化和钝化的事件
创建person类实现HttpSessionActivationListener接口public class Person implements HttpSessionActivationListener, Serializable {
public void sessionDidActivate(HttpSessionEvent arg0) {
System.out.println("session从硬盘读入内存了");
}
public void sessionWillPassivate(HttpSessionEvent arg0) {
System.out.println("session被存到硬盘中去了");
}
}
index.jsp写入:
<%
session.setAttribute("person",new Person());
%>
启动服务访问index.jsp页面,等待一分钟,控制台打印了“session被存到硬盘中去了”然后再次刷新页面,控制台会打印“session从硬盘读入内存”再过一分钟控制台又会打印出“session被存到硬盘中去了”再次刷新会再打印“session从硬盘读入内存”
到服务器对应目录中查看钝化后的session文件
C:\apache-tomcat-7.0.22\work\Catalina\localhost\2011-11-24\class3g\AE40CDFE5F5018FAED45AACE025E4EC2.session
最后一个案例:显示登陆用户列表,并实现踢人功能:下载地址:http://download.csdn.net/detail/rhljiayou/3838511
来源:oschina
链接:https://my.oschina.net/u/139546/blog/374067