监听域对象中属性变更的监听器

域对象中属性的变更的事件监听器就是用来监听ServletContext、HttpSession、HttpServletRequest这三个对象中的属性变更信息事件的监听器。

这三个监听器接口分别是ServletContextAttributeListener、HttpSessionAttributeListener和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加、删除和替换的事件,同一事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

●  attributeAdded方法

当向被监听对象中增加一个属性时,web容器就调用事件监听器的attributeAdded方法进行响应,这个方法接收一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象。

各个域属性监听器中的完整语法定义为:

public void attributeAdded(ServletContextAttributeEvent event)

public void attributeAdded(HttpSessionBindingEvent event)

public void attributeRemove(ServletRequestAttributeEvent event)

●  attributeRemove方法

当删除被监听对象中的一个属性时,web容器调用事件监听器的attributeRemoved方法进行响应。

各个域属性监听器中的完整语法定义为:

public void attributeRemoved(ServletContextAttributeEvent event)

public void attributeRemoved(HttpSessionBindingEvent event)

public void attributeRemoved(ServletRequestAttributeEvent event)

●  attributeReplaced方法

●  当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的attributeReplaced方法进行响应。

各个域属性监听器中的完整语法定义为:

public void attributeReplaced(ServletContextAttributeEvent event)

public void attributeReplaced(HttpSessionBindingEvent event)

public void attributeReplaced(ServletRequestAttributeEvent event)

ServletContextAttributeListener监听器范例:

●  编写ServletContextAttributeListener监听器监听ServletContext域对象的属性值变化情况,代码如下:

package com.xdl.listener;

import java.text.MessageFormat;

import javax.servlet.ServletContextAttributeEvent;

import javax.servlet.ServletContextAttributeListener;

/**

* MyServletContextAttributeListener类实现了

* ServletContextAttributeListener接口

* 因此可以对ServletContext域对象中属性的变更进行监听

*/

public class MyServletContextAttributeListener

implements ServletContextAttributeListener {

@Override

public void attributeAdded(ServletContextAttributeEvent event) {

String str = MessageFormat.format(

"ServletContext 域对象中添加了属性:{0},属性值是:{1}",

event.getName(), event.getValue());

System.out.println(str);

}

@Override

public void attributeRemoved(ServletContextAttributeEvent event) {

String str = MessageFormat.format(

"ServletContext 域对象中删除了属性:{0},属性值是:{1}",

event.getName(), event.getValue());

System.out.println(str);

}

public void attributeReplaced(ServletContextAttributeEvent event) {

String str = MessageFormat.format(

"ServletContext 域对象中替换了属性:{0}的值", event.getName());

System.out.println(str);

}

}

●  在web.xml文件中注册监听器

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

<listener>

<description>MyServletContextAttributeListener监听器</description>

<listener-class>

com.xdl.listener.MyServletContextAttributeListener

</listener-class>

</listener>

</web-app>

●  编写ServletContextAttributeListenerTest.jsp测试页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>兄弟连IT教育</title>

</head>

<body>

<%

//向application域对象中添加属性

application.setAttribute("name", "三十画生");

//替换application域对象中name属性的值

application.setAttribute("name","二十画生");

//移除application域对象中name的属性

application.removeAttribute("name");

%>

</body>

</html>

打开Tomcat服务器,运行结果如图12所示。

图12  MyServletContextAttributeListener在控制台中输出的信息

从运行结果中可以看到,ServletContextListener监听器成功监听到了ServletContext域对象(application)中属性值的变化情况。

ServletRequestAttributeListener和HttpSessionAttributeListenenr监听器范例:

●  编写监听器监听HttpSession和HttpServletRequest域对象的属性值变化情况,代码如下:

package com.xdl.listener;

import java.text.MessageFormat;

import javax.servlet.ServletRequestAttributeEvent;

import javax.servlet.ServletRequestAttributeListener;

import javax.servlet.http.HttpSessionAttributeListener;

import javax.servlet.http.HttpSessionBindingEvent;

/**

* MyRequestAndSessionAttributeListener 类实现了

* HttpSessionAttributeListener和ServletRequestAttributeListener接口

* 因此可以对ServletRequest和HttpSession 域对象中属性的变更进行监听

*/

public class MyRequestAndSessionAttributeListener

implements HttpSessionAttributeListener, ServletRequestAttributeListener {

@Override

public void attributeAdded(ServletRequestAttributeEvent event) {

String str = MessageFormat.format(

"ServletRequest 域对象中添加了属性:{0},属性值是:{1}",

event.getName(), event.getValue());

System.out.println(str);

}

@Override

public void attributeRemoved(ServletRequestAttributeEvent event) {

String str = MessageFormat.format(

"ServletRequest 域对象中删除了属性:{0},属性值是:{1}",

event.getName(), event.getValue());

System.out.println(str);

}

@Override

public void attributeReplaced(ServletRequestAttributeEvent event) {

String str = MessageFormat.format(

"ServletRequest 域对象中替换了属性:{0}的值", event.getName());

System.out.println(str);

}

@Override

public void attributeAdded(HttpSessionBindingEvent event) {

String str = MessageFormat.format(

"HttpSession 域对象中添加了属性:{0},属性值是:{1}",

event.getName(), event.getValue());

System.out.println(str);

}

@Override

public void attributeRemoved(HttpSessionBindingEvent event) {

String str = MessageFormat.format(

"HttpSession 域对象中删除了属性:{0},属性值是:{1}",

event.getName(), event.getValue());

System.out.println(str);

}

@Override

public void attributeReplaced(HttpSessionBindingEvent event) {

String str = MessageFormat.format(

"HttpSession 域对象中替换了属性:{0}的值", event.getName());

System.out.println(str);

}

}

●  在web.xml文件中注册监听器

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

<listener>

<description>MyRequestAndSessionAttributeListener监听器</description>

<listener-class>

com.xdl.listener.MyRequestAndSessionAttribute Listener

</listener-class>

</listener>

</web-app>

●  编写RequestAndSessionAttributeListenerTest.jsp测试页面

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>兄弟连IT教育</title>

</head>

<body>

<%

//向session域对象中添加属性

session.setAttribute("name","三十画生");

//替换session域对象中name属性的值

session.setAttribute("name", "二十画生");

//移除session域对象中name属性

session.removeAttribute("name");

//向request域对象中添加属性

request.setAttribute("name", "三十画生");

//替换request域对象中name属性的值

request.setAttribute("name", "二十画生");

//移除request域对象中name属性

request.removeAttribute("name");

%>

</body>

</html>

打开Tomcat服务器,运行结果如图13所示。

图13  MyRequestAndSessionAttributeListener在控制台中输出的信息

从运行结果中可以看到,HttpSessionAttributeListeren监听器和ServletRequestAttribute Listeren监听器成功监听到了HttpSession域对象和HttpServletRequest域对象的属性值变化情况。

IT兄弟连 JavaWeb教程 监听器3的更多相关文章

  1. IT兄弟连 JavaWeb教程 监听器4

    感知Session绑定事件的监听器 保存在Session域中的对象可以有多种状态:绑定(session.setAttribute("bean",Object)到Session中:从 ...

  2. IT兄弟连 JavaWeb教程 监听器2

    4  监听HttpSession域对象的创建和销毁 HttpSessionListener接口用于监听HttpSession对象的创建和销毁. 创建一个Session时,激发sessionCreate ...

  3. IT兄弟连 JavaWeb教程 监听器1

    1  基本概念 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其实就是一个实现特定接口的普通java程序,这个程序 ...

  4. IT兄弟连 JavaWeb教程 过滤器与监听器经典面试题

    1.谈谈你对Servlet过滤器的理解 过滤器是Servlet2.3规范中定义的一种小型的.可插入的Web组件.用来拦截Servlet容器的请求和响应过程,以便查看.提取客户端和服务器之间正在交换的数 ...

  5. IT兄弟连 JavaWeb教程 过滤器1

    Servlet过滤器是从Servlet2.3规范开始新增的功能,并在Servlet2.4规范中得到增强,监听器可以监听到Web应用程序启动和关闭.创建过滤器和监听器需要继承相应接口,并对其进行配置. ...

  6. IT兄弟连 JavaWeb教程 ServletContext对象

    ServletContext是Servlet与Servlet容器之间直接通信的接口.Servlet容器在启动一个Web应用时,会为它创建一个ServletContext对象.每个Web应用都有唯一的S ...

  7. IT兄弟连 JavaWeb教程 JSON和JSON字符串

    JSON (JavaScript Object Notation)是JavaScript语言中的一种对象类型.JSON的好处是易于阅读和解析.当客户端和服务器端需要交互大量数据时,使用JSON格式传输 ...

  8. IT兄弟连 JavaWeb教程 文件下载技术

    ●  列出提供下载的文件资源 我们要将Web应用系统中的文件资源提供给用户进行下载,首先我们要有一个页面列出上传文件目录下的所有文件,当用户点击文件下载超链接时就进行下载操作,编写一个ListFile ...

  9. IT兄弟连 JavaWeb教程 文件上传技术

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参 ...

随机推荐

  1. STemWin显示汉字 — SD卡外挂XBF字库

    转载注明出处  方法来自安福莱教程 1: 使用emWin自带小工具生成字库 (1)启动软件 选择4位抗锯齿 (2)根据需求选择字体类型和字体大小 (3)另存为XBF格式 2: 创建XBF字体 #inc ...

  2. shell的面试题

    在网上看到一道考shell的题--统计出一个文件夹下大小大于7MB的文件夹 好,我也来装一下深沉,个人觉得,这道题主要考的是对经常使用的linux的熟练程度(我不是很熟,哈哈),题目要统计,主要用到两 ...

  3. 《CSS权威指南(第三版)》---第五章 字体

    这章主要的内容有: 1.字体:一般使用一种通用的字体. 2.字体加粗:一般从数字100 -900 . 3.字体大小:font-size 4.拉伸和调整字体:font-stretch 5.调整字体大小: ...

  4. ActiveMQ持久化机制

    用户注册成功后发短信提醒 同步http 异步mq JMS中两种通讯模式: 发布订阅   一对多  topic   去过消费者集群的话 都会消费 消息队列   点对点 queue  去过消费者集群的话 ...

  5. 安装NXNET

    cran <- getOption("repos") cran["dmlc"] <- "https://s3-us-west-2.amaz ...

  6. iOS Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:]

    Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:], /SourceCache/UIKit_Sim/UIKit-3 ...

  7. 搭建LoadRunner中的场景(一) 创建场景

    一.创建场景 1. 使用场景创建设置对话框 场景分类: 1. 人工场景:相比面向目标场景,人工场景在实际工作中的应用更为广泛. 2. 面向目标场景:预先定义了一个测试目标,LoadRunner将根据这 ...

  8. Windows内存性能分析(二)性能瓶颈

    内存瓶颈: 由于可用内存缺乏导致系统性能下降的现像. (一).相关的性能对象 主要考虑内存的页面操作和磁盘的I/O操作,需要考虑如下性能对象: Memory性能对象: 用于分析整个系统的内存瓶颈问题. ...

  9. python练习1(简单爬虫)

    做一个简单的练习 目标:爬取中文小说 目标网站:http://www.biqule.com/book_58/26986.html 只爬取正文部分. 使用requests库来获取网页信息,使用re库正则 ...

  10. Listen81

    Nut-Cracking Chimps Demonstrate Cultural Differences One family generally dines on Chinese takeout w ...