Listener 监听器

前言:之前写了一篇关于Filter的文章:,现在再来一篇Listener的,Filter和Listener在项目中是经常用到的,巧妙的使用可以达到事半功倍的效果。故把两者的用法总结一下。

一、Listener的定义与作用

监听器Listener就是在application,session,request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件。

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

二、Listener的分类与使用

主要有以下三类:

1、ServletContext监听

ServletContextListener:用于对Servlet整个上下文进行监听(创建、销毁)。

contextDestroyed(ServletContextEvent sce);ServletContext getServletContext();//ServletContextEvent事件:取得一个ServletContext(application)对象

ServletContextAttributeListener:对Servlet上下文属性的监听(增删改属性)。

attributeRemoved(ServletContextAttributeEvent scab);attributeRepalced(ServletContextAttributeEvent scab);//属性替换(第二次设置同一属性)String getName();Object getValue();//取得属性的值

2、Session监听

Session属于http协议下的内容,接口位于javax.servlet.http.*包下。

HttpSessionListener接口:对Session的整体状态的监听。

sessionDestroyed(HttpSessionEvent se);//session销毁HttpSession getSession();//取得当前操作的session

HttpSessionAttributeListener接口:对session的属性监听。

attributeRemoved(HttpSessionBindingEvent se);attributeReplaced(HttpSessionBindingEvent se);//替换属性String getName();Object getValue();HttpSession getSession();//取得当前的session

session的销毁有两种情况:

1、session超时,web.xml配置:

120

2、手工使session失效

3、Request监听

ServletRequestListener:用于对Request请求进行监听(创建、销毁)。

requestDestroyed(ServletRequestEvent sre);//request销毁ServletRequest getServletRequest();ServletContext getServletContext();//取得一个ServletContext(application)对象

attributeRemoved(ServletRequestAttributeEvent srae);attributeReplaced(ServletRequestAttributeEvent srae);//属性替换(第二次设置同一属性)String getName();Object getValue();//取得属性的值

4、在web.xml中配置

Listener配置信息必须在Filter和Servlet配置之前,Listener的初始化(ServletContentListener初始化)比Servlet和Filter都优先,而销毁比Servlet和Filter都慢。

com.listener.class

三、Listener应用实例

1、利用HttpSessionListener统计最多在线用户人数

import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.ServletContext;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class HttpSessionListenerImpl implements HttpSessionListener {public void sessionCreated(HttpSessionEvent event) {ServletContext app = event.getSession().getServletContext();int count = Integer.parseInt(app.getAttribute(“onLineCount”).toString());count++;app.setAttribute(“onLineCount”, count);int maxOnLineCount = Integer.parseInt(app.getAttribute(“maxOnLineCount”).toString());if (count > maxOnLineCount) {//记录最多人数是多少app.setAttribute(“maxOnLineCount”, count);DateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);//记录在那个时刻达到上限app.setAttribute(“date”, df.format(new Date()));}} sessionDestroyed(HttpSessionEvent event) {ServletContext app = event.getSession().getServletContext();int count = Integer.parseInt(app.getAttribute(“onLineCount”).toString());count–;app.setAttribute(“onLineCount”, count);}}

2、Spring使用ContextLoaderListener加载ApplicationContext配置信息

ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

ContextLoaderListener如何查找ApplicationContext.xml的配置位置以及配置多个xml:如果在web.xml中不写任何参数配置信息,默认的路径是”/WEB-INF/applicationContext.xml”,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml(在MyEclipse中把xml文件放置在src目录下)。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数。

contextConfigLocationclasspath:spring/applicationContext-*.xmlorg.springframework.web.context.ContextLoaderListener

3、Spring使用Log4jConfigListener配置Log4j日志

Spring使用Log4jConfigListener的好处:

1. 动态的改变记录级别和策略,不需要重启Web应用。

到尽头,也许快乐,或有时孤独,如果心在远方,

Listener 监听器

相关文章:

你感兴趣的文章:

标签云: