Servlet监听器

本文深入探讨了Servlet监听器的概念及其在Java Web开发中的作用,包括观察者模式的应用、常用监听接口(如ServletContextListener、HttpSessionListener等)的功能与使用示例,以及如何配置监听器。通过具体实例演示了如何利用监听器实现特定操作,如监控在线用户数、HTTP会话创建与销毁等。

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

 

转载于:https://www.cnblogs.com/liu-Gray/p/4899727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值