2019独角兽企业重金招聘Python工程师标准>>>

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

域对象中属性的变更的事件监听器就是用来监听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域对象的属性值变化情况。

转载于:https://my.oschina.net/u/4125915/blog/3057259

IT兄弟连 JavaWeb教程 监听器3相关推荐

  1. java web 请求跟踪_IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie技术

    原标题:IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie技术 Cookie使用HTTPHeader传递数据.Cookie机制定义了两种报头,Set-Cookie报头和Cookie报 ...

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

    2019独角兽企业重金招聘Python工程师标准>>> JSON (JavaScript Object Notation)是JavaScript语言中的一种对象类型.JSON的好处是 ...

  3. IT兄弟连 JavaWeb教程 MVC设计模式

    MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它强制性地把应用程序的数据展示.数据处理和流程控制分开.MVC把应用程序分成3个核心模块:模型.视 ...

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

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

  5. IT兄弟连 JavaWeb教程 Servlet转发

    2019独角兽企业重金招聘Python工程师标准>>> Servlet对象由Servlet容器创建,并且Servlet对象的service()方法也由容器调用,一个Servlet对象 ...

  6. IT兄弟连 JavaWeb教程 jQuery中其他AJAX支持的函数

    ● $.get()函数 $.get(url,data,function,dataType);参数说明如下: url:请求地址 data:请求参数 dataType:服务器返回的数据类型 functio ...

  7. IT兄弟连 JavaWeb教程 EL表达式获取对象的属性以及数组的元素

    使用${对象名.属性名} EL表达式语言可以使用点号运算符"."来访问对象的属性,例如表达式${customer.name}表示customer对象的name属性. 使用${对象名 ...

  8. IT兄弟连 JavaWeb教程 jQuery对AJAX的支持经典案例

    案例需求:编写用户登陆页面的验证码模块,在用户进行登陆时,输入验证码后不需要点击提交按钮,使用AJAX异步地向服务器发送验证验证码的请求.如果验证码正确,可以点击提交按钮,如果验证码输入错误,提示用户 ...

  9. IT兄弟连 JavaWeb教程 AJAX定义以及解决的问题

    2019独角兽企业重金招聘Python工程师标准>>> Ajax是"Asynchronous JavaScript And XML"的缩写(即:异步的JavaSc ...

最新文章

  1. FileSystemWatcher使用方法具体解释
  2. python讲1020逆序输出_手把手带你学 Python3(九)| 快速实现数据处理的不二工具(文末有彩蛋)...
  3. 求数组的子数组之和的最大值
  4. Distributed TensorFlow
  5. 如何将SAP Spartacus的默认home页面替换成login页面 - ProtectedRoutesService
  6. 「数据分析」Sqlserver中的窗口函数的精彩应用之数据差距与数据岛(含答案)...
  7. 一个切图仔的工作日常
  8. VMware Mac 全屏问题
  9. POJ3292 UVA11105 Semi-prime H-numbers【筛法打表】
  10. 论文解读——An Analysis of Scale Invariance in Object Detection – SNIP
  11. nginx的介绍和安装
  12. iOS:如何实现在文字上添加拼音
  13. 学习搭建谷粒商城微服务框架(Docker配置)-01
  14. 计算机桌面的快捷方式怎么打开方式,桌面快捷方式打不开,教您桌面快捷方式打不开怎么解决...
  15. xshell下载安装
  16. VS2015专业版打开处于白屏状态
  17. SSR服务端渲染(nuxt重构项目)
  18. openFeign夺命连环9问
  19. 鼠标使用板载内存和使用计算机上,鼠标怎么选?教你选择适合自己的游戏鼠标。...
  20. synaptics linux驱动程序,Synaptics

热门文章

  1. Android蓝牙开发其二
  2. 第十、十一周项目-阅读程序,写出这些程序的运行结果(2)
  3. Android开发之触摸事件处理机制详解
  4. Http和Https对比
  5. UITableViewHeader 动态调整高度
  6. NSJSONSerliazition文档翻译和使用
  7. (0043) iOS 开发之Xcode相关路径
  8. DirectX11 With Windows SDK--22 立方体映射:静态天空盒的读取与实现
  9. (一)安装docker
  10. MySQL主从复制之传统复制与GTID模式之间切换