无忧技术网 - RSS订阅 
无忧技术网

使用监听器Servlet


作者:[佚名] - 发布:2010-4-22 17:35:38 - 来源:无忧技术网

监听器概述


  1.Listener是Servlet的监听器 
  2.可以监听客户端的请求、服务端的操作等。
  3.通过监听器,可以自动激发一些操作,如监听在线用户数量,当增加一个HttpSession时,给在线人数加1。
  4.编写监听器需要实现相应的接口
  5.编写完成后在web.xml文件中配置一下,就可以起作用了
  6.可以在不修改现有系统基础上,增加web应用程序生命周期事件的跟踪

常用的监听接口


  1.ServletContextListener
  监听ServletContext的操作。
  当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;
  当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
  2.ServletContextAttributeListener
  监听对ServletContext属性的操作。
  当在ServletContext增加一个属性时,激发attributeAdded(ServletContextAttributeEvent scae)方法;
  当在ServletContext删除一个属性时,激发attributeRemoved(ServletContextAttributeEvent scae)方法;
  当在ServletContext属性被重新设置时,激发attributeReplaced(ServletContextAttributeEvent scae) 方法。
  3.HttpSessionListener
  监听HttpSession的操作。
  当创建一个Session时,激发sessionCreated(SessionEvent se)方法;
  当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
  4.HttpSessionAttributeListener
  监听对HttpSession中的属性的操作。
  当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;
  当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;
  当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

使用范例:

由监听器管理共享数据库连接



  生命周期事件的一个实际应用由context监听器管理共享数据库连接。在web.xml中如下定义监听器:
<listener>
    <listener-class>XXX.MyConnectionManager</listener-class>
</listener> Øserver创建监听器的实例,接受事件并自动判断实现监听器接口的类型。要记住的是由于监听器是配置在部署描述符web.xml中,所以不需要改变任何代码就可以添加新的监听器。

public class MyConnectionManager implements ServletContextListener{  
  public void contextInitialized(ServletContextEvent e) { 
        Connection con = // create connection 
        e.getServletContext().setAttribute("con", con); 
    }  
   public void contextDestroyed(ServletContextEvent e) { 
        Connection con = (Connection) e.getServletContext().getAttribute("con"); 
        try {
          con.close(); 
        } 
       catch (SQLException ignored) { } // close connection 
    } 
}  
  监听器保证每新生成一个servlet context都会有一个可用的数据库连接,并且所有的连接对会在context关闭的时候随之关闭。 

计算在线用户数量的Linstener


(1)
 Package xxx; 

 public class OnlineCounter {  
   private static long online = 0;     
   public static long getOnline(){
      return online;
    }
    public static void raise(){ 
       online++;
    }
    public static void reduce(){ 
       online--; 
   }


import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCounterListener implements HttpSessionListener{
    public void sessionCreated(HttpSessionEvent hse) { 
        OnlineCounter.raise();  
    } 
   public void sessionDestroyed(HttpSessionEvent hse){  
        OnlineCounter.reduce();
    } 
 } 

在需要显示在线人数的JSP中可是使用
目前在线人数:
<%@ page import=“xxx.OnlineCounter" %>
<%=OnlineCounter.getOnline()%>

退出会话(可以给用户提供一个注销按钮):
<form action="exit.jsp" method=post> 
  <input type=submit value="exit"> 
</form> 

exit.jsp: <%session.invalidate() ;%> 

在web.xml中加入:
<listener> 
  <listener-class>servletlistener111111.SecondListener</listener-class> </listener>

怎么样,就是这么简单,不用对现有代码做任何的修改。 
责任编辑:liqwei
打印本页】【关闭本页】【返回列表
·上一篇:程序员人生:J2ee的学习流程简介
·下一篇:Java入门需掌握的30个基本概念
 文章评分
  • current rating
-5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5
 相关文章
 相关评论
 站点最新文章 更多>> 
·[经典影音]弱点
·[经典影音]萨利机长
·[经典影音]天空之眼
·[管理知识]康奈尔笔记法,提高100%学习效率
·[管理知识]刘强东:我管75000人靠这4张表格
·[管理知识]跟壳牌学HSE管理
·[运营策划]编辑工作内容整理
·[至理名言]奋斗与决定
·[瀚海拾遗]盲人打灯笼之各家论道
·[搞笑段子]中国男足
 站点浏览最多 更多>> 
·[协议规范]http断点续传原理:http头 Range、…
·[JS/CSS/HTML]HTML 空格的表示符号 nbsp / en…
·[NoSQL]Mongo数据库简介
·[协议规范]什么是SPF记录?如何设置、检测SP…
·[协议规范]图解 HTTPS 通信过程
·[PHP]精选国外免费PHP空间推荐
·[程序综合]常用IP地址查询接口
·[程序综合]什么是 DNS Prefetch ?
·[程序综合]获取客户端IP地址的三个HTTP请求…
·[Linux]/usr 目录的由来