zoukankan      html  css  js  c++  java
  • Servlet监听器

    Servlet监听器

    Listener

      观察者模式。

      本博客中关于观察者模式的博文:

      http://www.cnblogs.com/mengdd/archive/2013/02/08/2909206.html

      其参考资料中列出了更多的博文。

     

      Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。

      通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。

    常用监听接口

    ServletContextListener

      监听ServletContext。

      当创建ServletContext时激发contextInitialized(ServletContextEvent sce);

      所有的ServletContextListeners会在所有filters和servlets初始化之前收到初始化通知。

      当销毁ServletContext时激发contextDestroyed(ServletContextEvent sce)。

      所有filters和servlets销毁之后,ServletContextListeners才得到context销毁通知。

      也即,ServletContextListeners是早出晚归型的。

    ServletContextAttributeListener

      监听对ServletContext属性的操作,比如增加、删除、修改属性。

      方法分别为:

      attributeAdded(ServletContextAttributeEvent event)

      attributeRemoved(ServletContextAttributeEvent event)

      attributeReplaced(ServletContextAttributeEvent event)

    HttpSessionListener

      监听HttpSession的操作。

      当创建一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,(这样就可以给在线人数加1)。

      当销毁一个Session时,激发sessionDestroyed(HttpSessionEvent se)方法。

    HttpSessionAttributeListener

      监听HttpSession的属性变化,监听增加、移除和修改事件:

      attributeAdded(HttpSessionBindingEvent event)

      attributeRemoved(HttpSessionBindingEvent event)

      attributeReplaced(HttpSessionBindingEvent event)

    使用实例

    实例1:ServletContextListener

    复制代码
    package com.mengdd.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    public class MyServletContextListener implements ServletContextListener {
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
    
            System.out.println("contextDestroyed: " + sce.getServletContext());
        }
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("contextInitialized: " + sce.getServletContext());
        }
    
    }
    复制代码

      配置在web.xml中:

        <listener>
            <listener-class>com.mengdd.listener.MyServletContextListener</listener-class>
        </listener>

     

    实例2:ServletContextAttributeListener

    复制代码
    package com.mengdd.listener;
    
    import javax.servlet.ServletContextAttributeEvent;
    import javax.servlet.ServletContextAttributeListener;
    
    public class MyServletContextAttributeListener implements
            ServletContextAttributeListener {
    
        @Override
        public void attributeAdded(ServletContextAttributeEvent event) {
    
            System.out.println("attributeAdded");
            System.out.println(event.getName() + " : " + event.getValue());
        }
    
        @Override
        public void attributeRemoved(ServletContextAttributeEvent event) {
            System.out.println("attributeRemoved");
            System.out.println(event.getName() + " : " + event.getValue());
        }
    
        @Override
        public void attributeReplaced(ServletContextAttributeEvent event) {
            System.out.println("attributeReplaced");
            System.out.println(event.getName() + " : " + event.getValue());
            // 注意Replaced方法参数的getValue()获取的是被替换掉的值,即上一个value
        }
    
    }
    复制代码

      配置:

        <listener>
            <listener-class>com.mengdd.listener.MyServletContextAttributeListener</listener-class>
        </listener>

      测试用JSP页面:

      页面1的body:

    复制代码
    <body>
        <%
            application.setAttribute("attr1", "hello");
        %>
        <%
            application.setAttribute("attr1", "world");
        %>
        <%
            application.setAttribute("attr1", "aa");
        %>
        <%
            application.setAttribute("attr1", "bb");
        %>
    </body>
    复制代码

      页面2的body:

    <body>
        <%
            application.removeAttribute("attr1");
        %>
    </body>

      HttpSessionListener、HttpSessionAttributeListener等的实例就不一一列举了。

    参考资料

      北京圣思园张龙老师Java Web视频教程。

     

  • 相关阅读:
    DynamicLibrary动态查询需要的一个CS 文件下载
    指定的命名连接在配置中找不到、非计划用于 EntityClient 提供程序或者无效
    ACCESS的一个相对好用的数据库连接字符串
    ASP.NET 入门 博客园文章 索引篇
    c# 最小化到系统栏,时钟,随机语句,程序发布 读书笔记本 (三)
    poj 2817 WordStack (状态dp)
    hdu 4380 Farmer Greedy (计算几何 2012 MultiUniversity Training Contest 9 )
    hdu 4353 Finding Mine (计算几何 2012 MultiUniversity Training Contest 6 )
    poj 3735 Training little cats (矩阵快速幂)
    hdu 4374 One hundred layer (dp +单调队列 2012 MultiUniversity Training Contest 8 )
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/4899727.html
Copyright © 2011-2022 走看看