基于Listener监听器生命周期(详解)

 更新时间:2017年10月18日 09:47:29   作者:小Cai先森  
下面小编就为大家带来一篇基于Listener监听器生命周期(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、Listener生命周期

listener是web三大组件之一,是servlet监听器,用来监听请求,监听服务端的操作。

listener分为:(都是接口类,必须实现相应方法)

1.生命周期监听器(3个)

ServletContextListener 
requestDestroyed 在容器启动时被调用(在servlet被实例化前执行)
requestInitialized 在容器销毁时调用(在servlet被销毁后执行)
HttpSessionListener
sessionCreated 在HttpSession创建后调用
sessionDestroyed 在HttpSession销毁前调用(执行session.invalidate();方法)
ServletRequestListener
requestDestroyed 在request对象创建后调用(发起请求)
requestInitialized 在request对象销毁前调用(请求结束)

2.属性变化监听器(3个)

attributeAdded(ServletContextAttributeEvent event)向appliction中添加属性时调用
attributeRemoved(ServletContextAttributeEvent event)从appliction中删除属性时调用
attributeReplaced(ServletContextAttributeEvent event)替换application中的属性时调用
HttpSessionAttributeListener
attributeAdded(HttpSessionBindingEvent event)
attributeRemoved(HttpSessionBindingEvent event)
attributeReplaced(HttpSessionBindingEvent event)
ServletRequestAttributeListener
attributeAdded(ServletRequestAttributeEvent event)
attributeRemoved(ServletRequestAttributeEvent event)
attributeReplaced(ServletRequestAttributeEvent event)

以上监听器接口除了传参不同,方法名都是一样的。分别监听application,session,request对象的属性变化。

3.session中指定类属性变化监听器(2)

HttpSessionBindingListener 
valueBound(HttpSessionBindingEvent event) 当该类实例设置进session域中时调用
valueUnbound(HttpSessionBindingEvent event) 当该类的实例从session域中移除时调用
HttpSessionActivationListener 
sessionWillPassivate(HttpSessionEvent se) 
sessionDidActivate(HttpSessionEvent se)

二、测试范例

1.生命周期监听:

ServletContentAttribute_Listener.java

public class ServletContentAttribute_Listener implements ServletContextListener {
 /**
  * ServletContextListener实现方法
  * @param sce
  */
 public void contextInitialized(ServletContextEvent sce) {
  System.out.println("ServletContextListener初始化");
 }

 public void contextDestroyed(ServletContextEvent sce) {
  System.out.println("ServletContextListener销毁");
 }
}

其他两个监听器类似,不在重复贴出。

在web.xml中配置

<!-- 监听器 -->
 <!-- servlet监听器 -->
 <listener>
 <listener-class>study.myListener.ServletContentAttribute_Listener</listener-class>
 </listener>

 <!-- session监听器 -->
 <listener>
 <listener-class>study.myListener.HttpSessionAttribute_Listener</listener-class>
 </listener>
 
 <!-- request监听器-->
 <listener>
 <listener-class>study.myListener.ServletRequestAttribute_Listener</listener-class>
 </listener>

运行结果:

 

  

2.属性监听:

ServletContentAttribute_Listener.java

public class ServletContentAttribute_Listener implements ServletContextAttributeListener{

 /**
  * ServletContextAttributeListener实现方法
  * @param event
  */
 public void attributeAdded(ServletContextAttributeEvent event) {
  String meg = MessageFormat.format("ServletContent添加属性:{0},属性值:{1}",event.getName(),event.getValue());
  System.out.println(meg);
 }

 public void attributeRemoved(ServletContextAttributeEvent event) {
  String meg = MessageFormat.format("ServletContent删除属性:{0},属性值:{1}",event.getName(),event.getValue());
  System.out.println(meg);
 }

 public void attributeReplaced(ServletContextAttributeEvent event) {
  String meg = MessageFormat.format("ServletContent替换属性:{0},属性值:{1}",event.getName(),event.getValue());
  System.out.println(meg);
 }

}

另外两个监听器类似,不在赘诉。接下来用jsp页面测试

listenerDemo.jsp

<%--
 Created by IntelliJ IDEA.
 User: Administrator
 Date: 2017/10/17
 Time: 15:28
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
 <title>监听器设置</title>
</head>
<body>
 <%
  /**
   * servlet监听
   */
  application.setAttribute("name","changxiang");
  application.setAttribute("name","小Cai先森");
  application.removeAttribute("name");

  /**
   * session监听
   */
  session.setAttribute("sessionName","changxiang");
  session.setAttribute("sessionName","小Cai先森");
  session.removeAttribute("sessionName");
  session.invalidate();
  /**
   * request监听
   */
  request.setAttribute("requestName","changxiang");
  request.setAttribute("requestName","小Cai先森");
  request.removeAttribute("requestName");
 %>
</body>
</html>

执行结果如下:

 

注意:其中遇到一个问题:就是在启动tomcat的时候servletcontextListener监听执行了两次,最后删除掉server.xml中 Context 的手动配置,这样就不会加载两次了。

以上这篇基于Listener监听器生命周期(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Hibernate原理及应用

    Hibernate原理及应用

    本文主要介绍了Hibernate原理及应用。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • java程序中protobuf的基本用法示例

    java程序中protobuf的基本用法示例

    这篇文章主要给大家介绍了关于java程序中protobuf的基本用法,protobuf 与json相比具有 压缩比高 、解压缩速度更快的优点,本文通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-08-08
  • Java Scala偏函数与偏应用函数超详细讲解

    Java Scala偏函数与偏应用函数超详细讲解

    Scala是一种多范式的编程语言,支持面向对象和函数式编程。Scala也支持异常处理,即在程序运行过程中发生意外或错误时,采取相应的措施
    2023-04-04
  • Java Stream流的常见生成和操作方法总结

    Java Stream流的常见生成和操作方法总结

    从Java1.8开始提出了Stream流的概念,本文将通过示例为大家详细讲解一下Stream流的常见生成和操作方法,感兴趣的小伙伴可以了解一下
    2022-09-09
  • Java实现顺序表和链表结构

    Java实现顺序表和链表结构

    大家好,本篇文章主要讲的是Java实现顺序表和链表结构,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • Mybatis以main方法形式调用dao层执行代码实例

    Mybatis以main方法形式调用dao层执行代码实例

    这篇文章主要介绍了Mybatis以main方法形式调用dao层执行代码实例,MyBatis 是一款优秀的持久层框架,MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作,需要的朋友可以参考下
    2023-08-08
  • 本地MinIO存储服务Java远程调用上传文件的操作过程

    本地MinIO存储服务Java远程调用上传文件的操作过程

    MinIO是一款高性能、分布式的对象存储系统,它可以100%的运行在标准硬件上,即X86等低成本机器也能够很好的运行MinIO,这篇文章主要介绍了本地MinIO存储服务Java远程调用上传文件的操作过程,需要的朋友可以参考下
    2023-11-11
  • tk.mybatis如何扩展自己的通用mapper

    tk.mybatis如何扩展自己的通用mapper

    这篇文章主要介绍了tk.mybatis如何扩展自己的通用mapper操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • SpringBoot 将多个Excel打包下载的实现示例

    SpringBoot 将多个Excel打包下载的实现示例

    本文主要介绍了SpringBoot 将多个Excel打包下载的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-12-12
  • Spring 中的 ResourceLoader实例详解

    Spring 中的 ResourceLoader实例详解

    Spring框架提供了ResourceLoader接口,用于加载资源文件,DefaultResourceLoader是其基本实现,只能加载单个资源,而ResourcePatternResolver继承自ResourceLoader,增加了按模式加载多个资源的能力,感兴趣的朋友一起看看吧
    2024-11-11

最新评论