---
title: "监听器"
author: "liuyuru"
date: "2010-11-16T19:58:21Z"
tags: ["Servlet", "Web", "XML", "应用服务器"]
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2010-11-16T19:58:21Z"
id: 813183
url: "https://liuyuru.iteye.com/blog/813183"
---

**监听器概述**

1.Listener是Servlet的监听器

2.可以监听客户端的请求、服务端的操作等。

3.通过监听器，可以自动激发一些操作，如监听在线用户数量，当增加一个HttpSession时，给在线人数加1。

4.编写监听器需要实现相应的接口

5.编写完成后在web.xml文件中配置一下，就可以起作用了

 

 

**常用的监听接口**

**应用事件模型提供了当ServletContext，HttpSession，ServletRequest状态改变时的通知功能。可以编写事件监听类来响应这些状态的改变，并且可以配置和部署应用事件和监听类到Web应用。**

对于**ServletContext**事件，当Web应用部署、卸载和对context增加属性时，事件监听类可以得到通知。下表列出了ServletContext的事件类型，对应特定事件的监听类必须实现的接口和当事件发生时调用的方法。

**事件类型**

**接口**

**方法**

**Servlet context被创建**

**javax.servlet.ServletContextListener**

**contextInitialized()**

**Servlet context被注销**

**javax.servlet.ServletContextListener**

**contextDestroyed()**

**增加属性**

**javax.servlet. ServletContextAttributeListener**

**attributeAdded()**

**删除属性**

**javax.servlet. ServletContextAttributeListener**

**attributeRemoved()**

**属性被替换**

**javax.servlet. ServletContextAttributeListener**

**attributeReplaced()**

 

**对于HttpSession事件，当session激活、删除或者session属性的增加、删除和替换时，事件监听类得到通知。**下表列出了HttpSession的事件类型，对应特定事件的监听类必须实现的接口和当事件发生时调用的方法。

**事件类型**

**接口**

**方法**

session激活

**javax.servlet.http. HttpSessionListener**

sessionCreated()

session删除

**javax.servlet.http. HttpSessionListener**

sessionDestroyed()

增加属性

javax.servlet.http. **HttpSessionAttributeListener**

attributeAdded()

删除属性

javax.servlet.http. HttpSessionAttributeListener

attributeRemoved()

属性被替换

javax.servlet.http. HttpSessionAttributeListener

attributeReplaced()

**对于ServletRequest事件，当request初始化、销毁或者request属性的增加、删除和替换时，事件监听类得到通知。下表列出了ServletRequest的事件类型，对应特定事件的监听类必须实现的接口和当事件发生时调用的方法。**

**事件类型**

**接口**

**方法**

Request初始化

javax.servlet**.ServletRequestListener**

requestInitialized()

request销毁

javax.servlet.ServletRequestListener

requestDestroyed()

增加属性

javax.servlet.**ServletRequestAttributeListener**

attributeAdded()

删除属性

javax.servlet.ServletRequestAttributeListener

attributeRemoved()

属性被替换

javax.servlet.ServletRequestAttributeListener

attributeReplaced()

[]() 

**配置事件监听类的步骤：**

&lt;!--\[if !supportLists]--&gt;1.     &lt;!--\[endif]--&gt;打开Web应用的部署描述文件web.xml

&lt;!--\[if !supportLists]--&gt;2.     &lt;!--\[endif]--&gt;增加事件声明标记&lt;listener&gt;。[**事件声明定义的事件监听类在事件发生时被调用。**]()**&lt;listener&gt;****标记必须在****&lt;filter&gt;****标记和****&lt;servlet&gt;****标记之****间****。**&lt;!--\[if !supportAnnotations]--&gt;[\[番茄花园1\]]()&lt;!--\[endif]--&gt; **可以为每种事件定义多个事件监听类，**Apusic应用服务器按照它们在部署描述文件声明的顺序调用。例如：

    &lt;listener&gt;

       &lt;listener-class&gt;

com.puckasoft.video.servlet.TestSessionListener

&lt;/listener-class&gt;

&lt;/listener&gt;编写和部署监听类。

#### []()编写事件监听类

编写事件监听类的步骤：

&lt;!--\[if !supportLists]--&gt;1.     &lt;!--\[endif]--&gt;创建新的类并实现事件对应的接口

&lt;!--\[if !supportLists]--&gt;2.     &lt;!--\[endif]--&gt;定义不接受参数、访问属性为public的构造函数

&lt;!--\[if !supportLists]--&gt;3.     &lt;!--\[endif]--&gt;实现接口的方法

&lt;!--\[if !supportLists]--&gt;4.     &lt;!--\[endif]--&gt;编译并拷贝到对应Web应用的WEB-INF/classes目录下，或者打包成jar文件拷贝到WEB-INF/lib目录下[]()

监控session创建和销毁的例子：

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

import com.puckasoft.video.util.InfoWebService;

public class TestSessionListener [**implements HttpSessionListener**]() &lt;!--\[if !supportAnnotations]--&gt;[\[p2\]]()&lt;!--\[endif]--&gt; {

      public void sessionCreated(HttpSessionEvent [arg0]()&lt;!--\[if !supportAnnotations]--&gt;[\[p3\]]()&lt;!--\[endif]--&gt; ) {

           System.out.println("系统创建了一个HttpSession对象");

           InfoWebService.addSessionNum();

      }

 

      public void [sessionDestroyed]()&lt;!--\[if !supportAnnotations]--&gt;[\[p4\]]()&lt;!--\[endif]--&gt; (HttpSessionEvent arg0) {

           // TODO Auto-generated method stub

           System.out.println("系统销毁了一个HttpSession对象");

           InfoWebService.decreaseSessionNum();

      }

 

}

 

其他知识点：

&lt;!--\[if !supportLists]--&gt;**1.**    &lt;!--\[endif]--&gt;**属性监听器中可以通过event.getName(),得到所创建属性的名称，event.getValue()属性得到所创建属性的值。**

&lt;!--\[if !supportLists]--&gt;**2.**    &lt;!--\[endif]--&gt;**使用某个监听器时，除了要在web.xml里配置，还要在使用监听器的页面里**&lt;%@ page language="java" import="java.util.\*,com.suppervideo.listener.UserAttrListener,com.suppervideo.listener.UserListener" pageEncoding="gbk"%&gt;**引入**

 

&lt;!--\[if !supportAnnotations]--&gt;

* * *

&lt;!--\[endif]--&gt;

&lt;!--\[if !supportAnnotations]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !supportAnnotations]--&gt;[]()&lt;!--\[endif]--&gt;

 &lt;!--\[if !supportAnnotations]--&gt;[\[番茄花园1\]]()&lt;!--\[endif]--&gt;注意&lt;listener&gt;的位置！！！

&lt;!--\[if !supportAnnotations]--&gt;

&lt;!--\[endif]--&gt;

&lt;!--\[if !supportAnnotations]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !supportAnnotations]--&gt;[]()&lt;!--\[endif]--&gt;

 &lt;!--\[if !supportAnnotations]--&gt;[\[p2\]]()&lt;!--\[endif]--&gt;监听器也是实现的接口

&lt;!--\[if !supportAnnotations]--&gt;

&lt;!--\[endif]--&gt;

&lt;!--\[if !supportAnnotations]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !supportAnnotations]--&gt;[]()&lt;!--\[endif]--&gt;

 &lt;!--\[if !supportAnnotations]--&gt;[\[p3\]]()&lt;!--\[endif]--&gt;一般把这个变量改为event

&lt;!--\[if !supportAnnotations]--&gt;

&lt;!--\[endif]--&gt;

&lt;!--\[if !supportAnnotations]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !supportAnnotations]--&gt;[]()&lt;!--\[endif]--&gt;

 &lt;!--\[if !supportAnnotations]--&gt;[\[p4\]]()&lt;!--\[endif]--&gt;Session失效时，调用；attributeRemoved是在session失效和属性被remove时

&lt;!--\[if !supportAnnotations]--&gt;

&lt;!--\[endif]--&gt;