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

域对象中属性的变更的事件监听器就是用来监听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. Java for LeetCode 125 Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. 《高性能Javascript》 Summary(二)

    第四章.算法和流程控制 Algorithms And Flow Control 原因:代码整体结构是执行速度的决定因素之一.代码量少不一定运行速度快,代码量多不一定运行速度慢.性能损失与组织代码和具体 ...

  3. SQLSERVER2008 R2的端口设置

    通过存储过程查看 我们首先打开sqlserver连接sqlserver2008的数据库实例,然后执行如下存储过程: --查询端口号 exec sys.sp_readerrorlog 0, 1, 'li ...

  4. 跟我一起学Git (十) Patches【转】

    本文转载自:http://cs-cjl.com/2014/05/05/learn_git_with_me_10 Git实现了以下三条用于交换patch的命令: git format-patch 用于创 ...

  5. SDUT OJ类型转换函数的应用

    题目描述 处理一个复数与一个double数相加的运算,结果存放在一个double型变量d1中,输出d1的值.定义Complex(复数)类,在成员函数中包含重载类型转换运算符:operator doub ...

  6. 分享知识-快乐自己:自定义struts2类型转换的全局与局部错误信息。

    遇到类型转换错误的时候(也就是说不能进行类型转换),struts2框架自动生成一条错误信息,并且将该错误信息放到addFieldError里面.我们可以通过配置文件来替换这条由struts2自动生成的 ...

  7. c# json 排版

    public static string PraseToJson(string json) { try { JsonSerializer s = new JsonSerializer(); JsonR ...

  8. codeforces 569B B. Inventory(水题)

    题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. 1139 First Contact(30 分)

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  10. 「51Nod1639」绑鞋带(概率

    1639 绑鞋带  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 有n根鞋带混在一起,现在重复n次以下操作:随机抽出两个鞋带头,把它们绑在一起.可 ...