监听器

监听器概述

1.ListenerServlet的监听器

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

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

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

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

 

 

常用的监听接口

应用事件模型提供了当ServletContextHttpSessionServletRequest状态改变时的通知功能。可以编写事件监听类来响应这些状态的改变,并且可以配置和部署应用事件和监听类到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()

 

配置事件监听类的步骤:

<!--[if !supportLists]-->1.     <!--[endif]-->打开Web应用的部署描述文件web.xml

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

    <listener>

       <listener-class>

com.puckasoft.video.servlet.TestSessionListener

</listener-class>

</listener>编写和部署监听类。

编写事件监听类

编写事件监听类的步骤:

<!--[if !supportLists]-->1.     <!--[endif]-->创建新的类并实现事件对应的接口

<!--[if !supportLists]-->2.     <!--[endif]-->定义不接受参数、访问属性为public的构造函数

<!--[if !supportLists]-->3.     <!--[endif]-->实现接口的方法

<!--[if !supportLists]-->4.     <!--[endif]-->编译并拷贝到对应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 <!--[if !supportAnnotations]-->[p2]<!--[endif]--> {

      public void sessionCreated(HttpSessionEvent arg0<!--[if !supportAnnotations]-->[p3]<!--[endif]--> ) {

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

           InfoWebService.addSessionNum();

      }

 

      public void sessionDestroyed<!--[if !supportAnnotations]-->[p4]<!--[endif]--> (HttpSessionEvent arg0) {

           // TODO Auto-generated method stub

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

           InfoWebService.decreaseSessionNum();

      }

 

}

 

其他知识点:

<!--[if !supportLists]-->1.    <!--[endif]-->属性监听器中可以通过event.getName(),得到所创建属性的名称,event.getValue()属性得到所创建属性的值。

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

 

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--> <!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[番茄花园1]<!--[endif]-->注意<listener>的位置!!!

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--> <!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[p2]<!--[endif]-->监听器也是实现的接口

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--> <!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[p3]<!--[endif]-->一般把这个变量改为event

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--> <!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[p4]<!--[endif]-->Session失效时,调用;attributeRemoved是在session失效和属性被remove

<!--[if !supportAnnotations]-->
<!--[endif]-->

你可能感兴趣的:(应用服务器,Web,xml,servlet)