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视频教程。

     

  • 相关阅读:
    easy ui 表单ajax和from两种提交数据方法
    easy ui 下拉级联效果 ,下拉框绑定数据select控件
    easy ui 下拉框绑定数据select控件
    easy ui 异步上传文件,跨域
    easy ui 菜单和按钮(Menu and Button)
    HTTP 错误 404.3
    EXTJS4.2 后台管理菜单栏
    HTML 背景图片自适应
    easy ui 表单元素input控件后面加说明(红色)
    EXTJS 4.2 添加滚动条
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/4899727.html
Copyright © 2011-2022 走看看