IT兄弟连 JavaWeb教程 监听器3

时间:2023-03-09 03:13:56
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所示。

IT兄弟连 JavaWeb教程 监听器3

图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所示。

IT兄弟连 JavaWeb教程 监听器3

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

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