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

域对象中属性的变更的事件监听器就是用来监听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. 怎样避免C#中将小数转换为字符串时出现科学记数法

    在C#中如果float.double.decimal类型的值,小数点后的0太多时,C#会用科学记数法来表示小数的值. 例如下面的double类型0.00009,如果我们直接将其用ToString()方 ...

  2. 20145239 《Java程序设计》第9周学习总结

    20145239 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC入门 JDBC简介 1.JDBC是java联机数据库的标准规范,它定义了一组标准类与接口,应用程序需要联机数 ...

  3. 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---2

    11-3:字符串 输出 三个标准的函数:puts() fputs() printf() 1.puts()函数: #include<stdio.h> #define DEF "I ...

  4. make和rest用法

    位置(position):下一个要读取或写入的数据的索引.缓冲区的位置不能为负,并且不能大于其限制(limit). 标记(mark)与重置(reset):标记是一个索引,通过Buffer中的mark( ...

  5. access 驱动在win64位出现问题

    如果是调试的话,将应用程序池的 启动win32应用程序池 为 true

  6. centos7在VMware下配置网络连接

    安装成功以后,首先更改vmwar的虚拟网络设置 1.参考连接:http://www.cnblogs.com/liongis/p/3265458.html 2.然后将虚拟机的设置里面将网络配置的连接方式 ...

  7. hadoop源码剖析--hdfs安全模式

    一.什么是安全模式 hadoop安全模式是name node的一种状态,处于该状态时有种量特性: 1.namenode不接受任何对hfds文件系统的改变操作(即此时整个文件系统处于只读状态): 2.不 ...

  8. HDU-6035:Colorful Tree(虚树+DP)

    这里有三道长得像的题: 一:HDU6036: There is a tree with nn nodes, each of which has a type of color represented ...

  9. 几个重要的 ASM Disk Groups 参数

    几个重要的Disk group 属性: 1. ACCESS_CONTROL.ENABLED该属性用来控制某个disk group 上ASM FILE Access Control. 该参数有2个值:t ...

  10. css3渐变gradient

    参考: http://www.w3cplus.com/content/css3-gradient