本文以二个Listener实例来讲述ServletContext、HttpSession对象生命周期及ServletContext、HttpSession对象中属性变化情况。
实例一:
用于监听ServletContext对象生命周期及ServletContext对象中属性的变化情况的监听器ContextListener,分别实现了ServletContextListener,ServletContextAttributeListener接口。代码如下:
- package com.hc.znpb.servlet;
- import javax.servlet.ServletContextAttributeEvent;
- import javax.servlet.ServletContextAttributeListener;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- /**
- * ServletContext对象监听器.<br>
- *
- * 作用:监听ServletContext对象生命周期及ServletContext对象中属性变化情况
- *
- * <pre>
- *
- * Copy Right Information : HC
- *
- * Project : znpb
- *
- * JDK version used : jdk 1.5.12
- *
- * Version : 1.0
- *
- * Modification history : Sep 27, 2008
- * </pre>
- */
- public class ContextListener implements ServletContextListener,
- ServletContextAttributeListener {
- /**
- * 当应用关闭时将执行此方法
- */
- public void contextDestroyed(ServletContextEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println("【监听到】应用被关闭!");
- }
- /**
- * 当应用启动时将执行此方法
- */
- public void contextInitialized(ServletContextEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println("【监听到】应用被启动!");
- }
- /**
- * 当ServletContext对象中新增属性时将执行此方法
- */
- public void attributeAdded(ServletContextAttributeEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println("【监听到】ServletContext对象中新增一名为" + arg0.getName()
- + "的属性,其属性值为:" + arg0.getValue());
- }
- /**
- * 当ServletContext对象中删除属性时将执行此方法
- */
- public void attributeRemoved(ServletContextAttributeEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println("【监听到】ServletContext对象中一名为" + arg0.getName()
- + "的属性被删除!");
- }
- /**
- * 当ServletContext对象中更新属性时将执行此方法
- */
- public void attributeReplaced(ServletContextAttributeEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println("【监听到】ServletContext对象中一名为" + arg0.getName()
- + "的属性被更新!");
- }
- }
实例二:
用于监听HttpSession对象生命周期及HttpSession对象中属性的变化情况的监听器SessionListener,分别实现了HttpSessionListener,HttpSessionAttributeListener接口。代码如下:
- package com.hc.znpb.servlet;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- import com.hc.znpb.util.SysUtil;
- /**
- *
- * Session监听器.<br>
- *
- * 作用:用于监听HttpSession对象的生命周期及HttpSession对象中属性变化情况
- *
- * <pre>
- *
- * Copy Right Information : HC
- *
- * Project : znpb
- *
- * JDK version used : jdk 1.5.12
- *
- * Version : 1.0
- *
- * Modification history : Sep 27, 2008
- * </pre>
- */
- public class SessionListener implements HttpSessionAttributeListener,
- HttpSessionListener {
- // 在线人数统计
- private int userCount = 0;
- /**
- * 当HttpSession对象中新增属性时将执行此方法
- */
- public void attributeAdded(HttpSessionBindingEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println("【监听到】HttpSession对象中新增一名为" + arg0.getName()
- + "的属性,其属性值为" + arg0.getValue());
- }
- /**
- * 当HttpSession对象中删除属性时将执行些方法
- */
- public void attributeRemoved(HttpSessionBindingEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println("【监听到】HttpSession对象中一名为" + arg0.getName()
- + "的属性被删除!");
- }
- /**
- * 当HttpSession对象中更新属性时将执行些方法
- */
- public void attributeReplaced(HttpSessionBindingEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println("【监听到】HttpSession对象中一名为" + arg0.getName()
- + "的属性被修改!");
- }
- /**
- * 当新生一个新的HttpSession对象(新用户上线)时执行此方法
- */
- public void sessionCreated(HttpSessionEvent arg0) {
- // TODO Auto-generated method stub
- // 在线人数加1
- arg0.getSession().setAttribute(SysUtil.SESSION_COUNT_USERS,
- new Integer(this.userCount++));
- System.out.println("【监听到】新用户" + arg0.getSession().getId() + "上线!");
- System.out.println("【在线用户数】" + this.userCount + "人");
- }
- /**
- * 当新生一个HttpSession对象销毁(新用户下线)时执行此方法
- */
- public void sessionDestroyed(HttpSessionEvent arg0) {
- // TODO Auto-generated method stub
- // 在线人数减1
- arg0.getSession().setAttribute(SysUtil.SESSION_COUNT_USERS,
- new Integer(--this.userCount));
- System.out.println("【监听到】新用户" + arg0.getSession().getId() + "下线!");
- System.out.println("【在线用户数】" + this.userCount + "人");
- }
- }
最后修改web.xml文件,如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <listener>
- <listener-class>
- com.hc.znpb.servlet.ContextListener
- </listener-class>
- </listener>
- <listener>
- <listener-class>
- com.hc.znpb.servlet.SessionListener
- </listener-class>
- </listener>
- </web-app>