IT兄弟连 JavaWeb教程 监听器3
监听域对象中属性变更的监听器
域对象中属性的变更的事件监听器就是用来监听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的更多相关文章
- IT兄弟连 JavaWeb教程 监听器4
感知Session绑定事件的监听器 保存在Session域中的对象可以有多种状态:绑定(session.setAttribute("bean",Object)到Session中:从 ...
- IT兄弟连 JavaWeb教程 监听器2
4 监听HttpSession域对象的创建和销毁 HttpSessionListener接口用于监听HttpSession对象的创建和销毁. 创建一个Session时,激发sessionCreate ...
- IT兄弟连 JavaWeb教程 监听器1
1 基本概念 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其实就是一个实现特定接口的普通java程序,这个程序 ...
- IT兄弟连 JavaWeb教程 过滤器与监听器经典面试题
1.谈谈你对Servlet过滤器的理解 过滤器是Servlet2.3规范中定义的一种小型的.可插入的Web组件.用来拦截Servlet容器的请求和响应过程,以便查看.提取客户端和服务器之间正在交换的数 ...
- IT兄弟连 JavaWeb教程 过滤器1
Servlet过滤器是从Servlet2.3规范开始新增的功能,并在Servlet2.4规范中得到增强,监听器可以监听到Web应用程序启动和关闭.创建过滤器和监听器需要继承相应接口,并对其进行配置. ...
- IT兄弟连 JavaWeb教程 ServletContext对象
ServletContext是Servlet与Servlet容器之间直接通信的接口.Servlet容器在启动一个Web应用时,会为它创建一个ServletContext对象.每个Web应用都有唯一的S ...
- IT兄弟连 JavaWeb教程 JSON和JSON字符串
JSON (JavaScript Object Notation)是JavaScript语言中的一种对象类型.JSON的好处是易于阅读和解析.当客户端和服务器端需要交互大量数据时,使用JSON格式传输 ...
- IT兄弟连 JavaWeb教程 文件下载技术
● 列出提供下载的文件资源 我们要将Web应用系统中的文件资源提供给用户进行下载,首先我们要有一个页面列出上传文件目录下的所有文件,当用户点击文件下载超链接时就进行下载操作,编写一个ListFile ...
- IT兄弟连 JavaWeb教程 文件上传技术
在Web应用系统开发中,文件上传和下载功能是非常常用的功能. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参 ...
随机推荐
- em、pt、px和百分比
浏览器默认的字体大小为100%=16px=12pt=1em px像素(Pixel):是固定大小的单元.相对长度单位.像素px是相对于显示器屏幕分辨率而言的.一个像素等于电脑屏幕上的一个点(是你屏幕分辨 ...
- <JAVA8新增内容>关于集合的操作(Collection/Iterator/Stream)
因为下文频繁使用lambda表达式,关于Java中的lambda表达式内容请见: http://www.cnblogs.com/guguli/p/4394676.html 一.使用增强的Iterato ...
- SDUT OJ 之 连通分量个数 (dfs)
数据结构实验:连通分量个数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 在无向图中,如果从顶点vi到顶点vj有路径,则称vi ...
- wordpress,cos-html-cache静态化后,点击数失效问题的解决方案
装了wordpress cos-html-cache 静态插件后,生成了静态文件,post-views等点击数插件就失效了, 找了一些,包括有个js版本的,需要用到post-views插件,我也不想装 ...
- ES设置字段搜索权重——Query-Time Boosting
Query-Time Boosting In Prioritizing Clauses, we explained how you could use the boost parameter at s ...
- ES BM25 TF-IDF相似度算法设置——
Pluggable Similarity Algorithms Before we move on from relevance and scoring, we will finish this ch ...
- Apache禁止或允许固定IP访问特定目录、文件、URL
1. 禁止访问某些文件/目录 增加Files选项来控制,比如要不允许访问 .inc 扩展名的文件,保护php类库: <Files ~ "\.inc$"> Order a ...
- Listen81
Nut-Cracking Chimps Demonstrate Cultural Differences One family generally dines on Chinese takeout w ...
- (转)C++经典面试题(最全,面中率最高)
1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应free只会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,new ...
- linux安装与卸载软件
在ubuntu系统中,通常使用apt-get命令完成对软件的安装与卸载 安装的软件通常都放置在一些源中,国内有很多镜像源供下载使用,而系统设置的源保存在目录/etc/apt/sources.list文 ...