J2EE中监听器Listener的应用

本文通过两个实例详细讲解了如何使用Listener监听ServletContext和HttpSession对象的生命周期以及属性变化,包括应用启动、关闭、属性增删改的监听,并提供了web.xml配置示例。
摘要由CSDN通过智能技术生成

本文以二个Listener实例来讲述ServletContext、HttpSession对象生命周期及ServletContext、HttpSession对象中属性变化情况。

 

 

实例一:

        用于监听ServletContext对象生命周期及ServletContext对象中属性的变化情况的监听器ContextListener,分别实现了ServletContextListener,ServletContextAttributeListener接口。代码如下:

  1. package com.hc.znpb.servlet;
  2. import javax.servlet.ServletContextAttributeEvent;
  3. import javax.servlet.ServletContextAttributeListener;
  4. import javax.servlet.ServletContextEvent;
  5. import javax.servlet.ServletContextListener;
  6. /**
  7.  * ServletContext对象监听器.<br>
  8.  * 
  9.  * 作用:监听ServletContext对象生命周期及ServletContext对象中属性变化情况
  10.  * 
  11.  * <pre>
  12.  * 
  13.  * Copy Right Information : HC
  14.  * 
  15.  * Project : znpb
  16.  * 
  17.  * JDK version used : jdk 1.5.12
  18.  * 
  19.  * Version : 1.0
  20.  * 
  21.  * Modification history : Sep 27, 2008
  22.  * </pre>
  23.  */
  24. public class ContextListener implements ServletContextListener,
  25.         ServletContextAttributeListener {
  26.     /**
  27.      * 当应用关闭时将执行此方法
  28.      */
  29.     public void contextDestroyed(ServletContextEvent arg0) {
  30.         // TODO Auto-generated method stub
  31.         System.out.println("【监听到】应用被关闭!");
  32.     }
  33.     /**
  34.      * 当应用启动时将执行此方法
  35.      */
  36.     public void contextInitialized(ServletContextEvent arg0) {
  37.         // TODO Auto-generated method stub
  38.         System.out.println("【监听到】应用被启动!");
  39.     }
  40.     /**
  41.      * 当ServletContext对象中新增属性时将执行此方法
  42.      */
  43.     public void attributeAdded(ServletContextAttributeEvent arg0) {
  44.         // TODO Auto-generated method stub
  45.         System.out.println("【监听到】ServletContext对象中新增一名为" + arg0.getName()
  46.                 + "的属性,其属性值为:" + arg0.getValue());
  47.     }
  48.     /**
  49.      * 当ServletContext对象中删除属性时将执行此方法
  50.      */
  51.     public void attributeRemoved(ServletContextAttributeEvent arg0) {
  52.         // TODO Auto-generated method stub
  53.         System.out.println("【监听到】ServletContext对象中一名为" + arg0.getName()
  54.                 + "的属性被删除!");
  55.     }
  56.     /**
  57.      * 当ServletContext对象中更新属性时将执行此方法
  58.      */
  59.     public void attributeReplaced(ServletContextAttributeEvent arg0) {
  60.         // TODO Auto-generated method stub
  61.         System.out.println("【监听到】ServletContext对象中一名为" + arg0.getName()
  62.                 + "的属性被更新!");
  63.     }
  64. }

实例二:

    用于监听HttpSession对象生命周期及HttpSession对象中属性的变化情况的监听器SessionListener,分别实现了HttpSessionListener,HttpSessionAttributeListener接口。代码如下:

  1. package com.hc.znpb.servlet;
  2. import javax.servlet.http.HttpSessionAttributeListener;
  3. import javax.servlet.http.HttpSessionBindingEvent;
  4. import javax.servlet.http.HttpSessionEvent;
  5. import javax.servlet.http.HttpSessionListener;
  6. import com.hc.znpb.util.SysUtil;
  7. /**
  8.  * 
  9.  * Session监听器.<br>
  10.  * 
  11.  * 作用:用于监听HttpSession对象的生命周期及HttpSession对象中属性变化情况
  12.  * 
  13.  * <pre>
  14.  * 
  15.  * Copy Right Information : HC
  16.  * 
  17.  * Project : znpb
  18.  * 
  19.  * JDK version used : jdk 1.5.12
  20.  * 
  21.  * Version : 1.0
  22.  * 
  23.  * Modification history : Sep 27, 2008
  24.  * </pre>
  25.  */
  26. public class SessionListener implements HttpSessionAttributeListener,
  27.         HttpSessionListener {
  28.     // 在线人数统计
  29.     private int userCount = 0;
  30.     /**
  31.      * 当HttpSession对象中新增属性时将执行此方法
  32.      */
  33.     public void attributeAdded(HttpSessionBindingEvent arg0) {
  34.         // TODO Auto-generated method stub
  35.         System.out.println("【监听到】HttpSession对象中新增一名为" + arg0.getName()
  36.                 + "的属性,其属性值为" + arg0.getValue());
  37.     }
  38.     /**
  39.      * 当HttpSession对象中删除属性时将执行些方法
  40.      */
  41.     public void attributeRemoved(HttpSessionBindingEvent arg0) {
  42.         // TODO Auto-generated method stub
  43.         System.out.println("【监听到】HttpSession对象中一名为" + arg0.getName()
  44.                 + "的属性被删除!");
  45.     }
  46.     /**
  47.      * 当HttpSession对象中更新属性时将执行些方法
  48.      */
  49.     public void attributeReplaced(HttpSessionBindingEvent arg0) {
  50.         // TODO Auto-generated method stub
  51.         System.out.println("【监听到】HttpSession对象中一名为" + arg0.getName()
  52.                 + "的属性被修改!");
  53.     }
  54.     /**
  55.      * 当新生一个新的HttpSession对象(新用户上线)时执行此方法
  56.      */
  57.     public void sessionCreated(HttpSessionEvent arg0) {
  58.         // TODO Auto-generated method stub
  59.         // 在线人数加1
  60.         arg0.getSession().setAttribute(SysUtil.SESSION_COUNT_USERS,
  61.                 new Integer(this.userCount++));
  62.         System.out.println("【监听到】新用户" + arg0.getSession().getId() + "上线!");
  63.         System.out.println("【在线用户数】" + this.userCount + "人");
  64.     }
  65.     /**
  66.      * 当新生一个HttpSession对象销毁(新用户下线)时执行此方法
  67.      */
  68.     public void sessionDestroyed(HttpSessionEvent arg0) {
  69.         // TODO Auto-generated method stub
  70.         // 在线人数减1
  71.         arg0.getSession().setAttribute(SysUtil.SESSION_COUNT_USERS,
  72.                 new Integer(--this.userCount));
  73.         System.out.println("【监听到】新用户" + arg0.getSession().getId() + "下线!");
  74.         System.out.println("【在线用户数】" + this.userCount + "人");
  75.     }
  76. }

 

最后修改web.xml文件,如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  5.     <listener>
  6.         <listener-class>
  7.             com.hc.znpb.servlet.ContextListener
  8.         </listener-class>
  9.     </listener>
  10.     <listener>
  11.         <listener-class>
  12.             com.hc.znpb.servlet.SessionListener
  13.         </listener-class>
  14.     </listener>
  15. </web-app>

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值