码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet监听器

时间:2015-10-22 06:45:24      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Servlet监听器

标签:

原文地址:http://www.cnblogs.com/liu-Gray/p/4899727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!