一、Listener生命周期

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

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

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

        • requestInitialized 在容器启动时被调用(在servlet被实例化前执行)
        • requestDestroyed 在容器销毁时调用(在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) 当对象session被序列化(钝化)后调用
        • sessionDidActivate(HttpSessionEvent se)  当对象session被反序列化(活化)后调用

            

二、测试范例

 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 的手动配置,这样就不会加载两次了。

解决思路:http://blog.csdn.net/shaokai132333/article/details/53328258

Listener监听器生命周期的更多相关文章

  1. filter和listener的生命周期

    filter(过滤器)和listener(监听器)的生命周期 老实说 生命周期要是说成作用的时间范围我会理解的更好 filter package com.javaweb.filter; import ...

  2. servelt filter listener 的生命周期

    1. servlet    当第一次请求一个servlet资源时,servlet容器创建这个servlet实例,并调用他的 init(ServletConfig config)做一些初始化的工作,然后 ...

  3. Java实现生命周期管理机制

    先扯再说 最近一直在研究某个国产开源的MySQL数据库中间件,拉下其最新版的代码到eclipse后,启动起来,然后做各种测试和代码追踪:用完想要关闭它时,拉出它的STOP类想要运行时,发现这个类里赫然 ...

  4. 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean

    1.有时候在项目中需要定时启动某个任务,对于这个需求,基于JavaEE规范,我们可以使用Listener与TimerTask来实现,代码如下: public class TestTaskListene ...

  5. [原创]java WEB学习笔记47:Servlet 监听器简介, ServletContext(Application 对象), HttpSession (Session 对象), HttpServletRequest (request 对象) 监听器,利用listener理解 三个对象的生命周期

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  6. JavaWEB 通过Listener理解域对象生命周期

    利用 ServletRequestListener.HttpSessionListener 以及 ServletContextListener 可以把request,session,以及Applica ...

  7. JavaEE(16) - JPA生命周期及监听器

    1. 理解实体的生命周期 2. 为实体生命周期事件定义监听器 3. 通过监听实现回调 4. 排除默认监听器和父类上定义的监听器 1. 理解实体的生命周期(Net Beans创建Java Project ...

  8. 在web.xml中配置监听器来控制ioc容器生命周期

    5.整合关键-在web.xml中配置监听器来控制ioc容器生命周期 原因: 1.配置的组件太多,需保障单实例 2.项目停止后,ioc容器也需要关掉,降低对内存资源的占用. 项目启动创建容器,项目停止销 ...

  9. 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)

    1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...

随机推荐

  1. navcat无法远程连接mysql数据库解决办法

    navcat无法远程连接mysql数据库,一般都是因为本地ip没有访问权限,服务器上执行下面指令即可解决 mysql -u root -p GRANT ALL PRIVILEGES ON *.* TO ...

  2. Linux 环境下程序不间断运行

    一.背景     在linux命令行中执行程序,程序通常会占用当前终端,如果不启动新的终端就没法执行其他操作.简单可以通过'&'将程序放到后台执行,但是这种方法有个问题就是,一旦连接远程服务器 ...

  3. CEF小白人系列2-DEV环境配置-Windows10+SDK+VS2015

    以下资料整理来自CEF官方教学.作者做了翻译和整理.并会以一种重事件操作的方式来一点点入门. 3个问题: 在上一篇内容中我们了解了什么是CEF,能干些什么.必要的物理学习环境. 下面我们也是以带着问题 ...

  4. Yaf框架的配置

    http://www.laruence.com/manual/yaf.ini.html //先看一下惠新宸鸟哥yaf官网的配置说明 我们可以在php.ini中定义开发环节配置项,把本地开发设置成dev ...

  5. uva11400 动态规划

    没种电压灯泡要么全换,要么不换.状态d(i)表示前i种灯泡的最低价格. 转移方程: dp[i]=min(dp[i],dp[j]+(s[i]-s[j])*d[i].c+d[i].k); AC代码: #i ...

  6. HDU - 1407 打表

    思路:预处理10000以内所有数的三平方和即可. AC代码 #include <cstdio> #include <cmath> #include <algorithm& ...

  7. Python使用Mysql官方驱动(取出dict类型的数据)

    简介 虽然Python有很多连接mysql的库,比如mysqldb, pymysql~这些都很方便,现在就教大家使用mysql的官方库来操作mysql. 安装 windows: 下载链接 选择自己的w ...

  8. python-scrapy的编码问题

    在学习scrapy中,遇到编码问题还是很头痛的问题的.由于对于语言的对不熟悉,加上不会思考的去解决问题.这样盲目的实践似乎就是在浪费时间. 用心思考一下是非常重要的一个过程,在没有办法前进的时候,先学 ...

  9. 20 个 CSS 高级技巧汇总

    原文:https://segmentfault.com/a/1190000003936841 使用技巧会让人变的越来越懒,没错,我就是想让你变懒.下面是我收集的CSS高级技巧,希望你懒出境界. 1. ...

  10. windowsXP下搭建JAVA环境教程

    一.工具准备安装JKD6:传送门:http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin-b32-w ...