`
wangyanlong0107
  • 浏览: 484565 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

【转】web.xml中listener的作用及使用(转)

 
阅读更多

概述:

Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。

接口:

目前Servlet2.4和JSP2.0总共有8个监听器接口和6个Event类,其中HttpSessionAttributeListener与

HttpSessionBindingListener 皆使用HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener则都使用HttpSessionEvent;其余Listener对应的Event如下所 示:

 

Listener接口

Event类

ServletContextListener

ServletContextEvent

ServletContextAttributeListener

ServletContextAttributeEvent

HttpSessionListener

HttpSessionEvent

HttpSessionActivationListener

HttpSessionAttributeListener

HttpSessionBindingEvent

HttpSessionBindingListener

ServletRequestListener

ServletRequestEvent

ServletRequestAttributeListener

ServletRequestAttributeEvent

分别介绍:

一 ServletContext相关监听接口

补充知识:

通过ServletContext 的实例可以存取应用程序的全局对象以及初始化阶段的变量。

在JSP文件中,application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。

注意:

全局对象即Application范围对象,初始化阶段的变量指在web.xml中,经由<context-param>元素所设定的变量,它的范围也是Application范围,例如:

<context-param>

<param-name>Name</param-name>

<param-value>browser</param-value>

</context-param>

当容器启动时,会建立一个Application范围的对象,若要在JSP网页中取得此变量时:

String name = (String)application.getInitParameter("Name");

或者使用EL时:

${initPara.name}

若是在Servlet中,取得Name的值方法:

String name = (String)ServletContext.getInitParameter("Name");

1.ServletContextListener

用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口。

ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。

ServletContextListener接口的方法:

void contextInitialized(ServletContextEvent sce)

通知正在接受的对象,应用程序已经被加载及初始化。

void contextDestroyed(ServletContextEvent sce)

通知正在接受的对象,应用程序已经被载出。

ServletContextEvent中的方法:

ServletContext getServletContext()

取得ServletContext对象

2.ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。

ServletContextAttributeListener接口方法:

void attributeAdded(ServletContextAttributeEvent scab)

若有对象加入Application的范围,通知正在收听的对象

void attributeRemoved(ServletContextAttributeEvent scab)

若有对象从Application的范围移除,通知正在收听的对象

void attributeReplaced(ServletContextAttributeEvent scab)

若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象

ServletContextAttributeEvent中的方法:

java.lang.String getName()

回传属性的名称

java.lang.Object getValue()

回传属性的值

二、HttpSession相关监听接口

1.HttpSessionBindingListener接口

注意:HttpSessionBindingListener接口是唯一不需要再web.xml中设定的Listener

当我们的类实现了HttpSessionBindingListener接口后,只要对象加入 Session范围 (即调用HttpSession对象的setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的 removeAttribute方法的时候或Session Time out的时候)时,容器分别会自动调用下列两个方法:

void valueBound(HttpSessionBindingEvent event)

void valueUnbound(HttpSessionBindingEvent event)

思考:如何实现记录网站的客户登录日志, 统计在线人数?

2.HttpSessionAttributeListener接口

HttpSessionAttributeListener监听HttpSession中的属性的操作。

当 在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。这和ServletContextAttributeListener比较类似。

3.HttpSessionListener接口

HttpSessionListener监听 HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。

4.HttpSessionActivationListener接口

主要用于同一个Session转移至不同的JVM的情形。

三、ServletRequest监听接口

1.ServletRequestListener接口

和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest

2.ServletRequestAttributeListener接口

和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest

四、举例:有的listener可用于统计网站在线人数及访问量。 如下:

服务器启动时(实现ServletContextListener监听器contextInitialized方法),读取数据库,并将其用一个计数变量保存在application范围内

session创建时(实现HttpSessionListener监听器sessionCreated方法),读取计数变量加1并重新保存

服务器关闭时(实现ServletContextListener监听器contextDestroyed方法),更新数据库

分享到:
评论

相关推荐

    web.xml 中的listener、 filter、servlet 加载顺序及其详解.doc

    web.xml 中的listener、 filter、servlet 加载顺序及其详解

    javaweb项目中web.xml的作用

    javaweb项目中web.xml的作用 web.xml是javaweb项目中一个非常重要的配置文件,它是每一个javaWeb工程都必需的配置文件。web.xml文件的主要作用是用于初始化工程配置信息,例如welcome页面、filter、listener、...

    Tomcat中用web.xml控制Web应用详解

    Tomcat 中 web.xml 文件是 Web 应用的核心配置文件,负责管理 Web 应用的生命周期、Servlet 的加载顺序、Filter 的配置等。下面对 web.xml 文件中的重要元素进行详细解释。 context-param 元素 context-param 元素...

    JSPservlet中web.xml详细配置指南(包含所有情况)

    本文将对 web.xml 的配置进行详细的解释,包括加载顺序、context-param、listener、filter、servlet 等配置节的使用和顺序。 加载顺序 ---------- 加载顺序是指 web.xml 文件中的元素出现的顺序,它对 Web 应用...

    关于web.xml配置文件servlet,filter,listener加载顺序

    关于filter、servlet在web.xml配置及加载顺序

    web.xml配置servlet,filter,listener加载顺序源代码

    本源码将详细介绍web.xml配置中servlet,filter,listener的加载顺序,可以让学习者更好的了解web.xml各种属性配置,自己写的东西,不足之处请大家见谅,顺便收点积分也好下资料,谢谢

    一篇关于web.xml配置的详细说明

    一篇关于web.xml配置的详细说明

    tomcat配置文件web.xml与server.xml解析

    在本文中,我们将对 web.xml 和 server.xml 中的主要配置项进行解析。 一、web.xml 配置文件 web.xml 配置文件是 Tomcat 服务器的主要配置文件,用于配置 Servlet、Servlet Mapping、Session 配置、MIME 类型映射...

    JSP Web.xml标准配置内容

    WEB.XML标准配置注解: &lt;!--Servlet的映射,用来说明客户端IE中输入什么样的地址字符串对应到哪个Servlet的别名--&gt; &lt;!--在该例中在IE地址栏中firstservlet字符串对应到别名为firstservlet的servlet--&gt; ...

    J2EE中关于web.xml文件的配置

    "J2EE 中关于 web.xml 文件的配置" 在 J2EE 中,web.xml 文件扮演着非常重要的角色,它是一个基于 XML 的配置文件,用于描述 Web 应用的各个方面的配置信息。通过 web.xml 文件,我们可以对 Web 应用进行配置,例如...

    Web.xml的作用及常用标签的功能

    web.xml文件是用来初始化工程配置信息的,比如说welcome页面,filter,listener,servlet,servlet-mapping,启动加载级别等等,当你的web工程中没用到这些当然也就不需要这个xml文件来配置你的apllication了 ...

    web.xml中ContextLoaderListener的运行过程解析

    web.xml中ContextLoaderListener的运行

    web.xml详解(web-app_2_3.dtd)

    部署描述文件web.xml详解(web-app_2_3.dtd),全面介绍: 1. icon元素 2. display-name元素 3. description元素 4. distributable元素 5. context-param元素 6. filter元素 7. filter-mapping元素 8. ...

    web.xml标签说明.docx

    在 Web.xml 文件中,我们可以找到许多重要的标签,下面将对这些标签进行详细的解释。 1. `&lt;description&gt;` 元素:用于为父元素提供一个文本描述。这个元素不仅可以在 `&lt;web-app&gt;` 元素中出现,还可以在其他多个元素...

    关于JSP配置文件web.xml加载顺序详解

    1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取和&lt;listener&gt;两个结点。 2、紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。 3、容器将转换为...

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part3

    本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。.  本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...

    Spring的监听器ContextLoaderListener的作用

    我们可以在 web.xml 文件中添加一段配置:&lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath*:applicationContext-*.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; ...

    spring和hibernate整合

    在web.Xml中 &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;/WEB-INF/spring/*.bean.xml &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; ...

    logback-ext-spring

    在web.xml中配置: &lt;param-name&gt;logbackConfigLocation &lt;param-value&gt;/WEB-INF/conf/logback.xml &lt;listener&gt; &lt;listener-class&gt;ch.qos.logback.ext.spring.web.LogbackConfigListener&lt;/listener-class&gt; &lt;/...

    Servlet中的八大Listener

    JSP/Servlet 中的事件处理写过AWT或...其实我们在编写JSP/Servle程序时,也有类似的事件处理机制,所不同的是在JSP/Servlet中是在web.xml中注册Listener,由Container在特定事件发生时呼叫特定的实现Listener的类。

Global site tag (gtag.js) - Google Analytics