IT兄弟連 JavaWeb教程 監聽器3
- 2019 年 10 月 5 日
- 筆記
版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/ITXDL123/article/details/90746107
監聽域對象中屬性變更的監聽器
域對象中屬性的變更的事件監聽器就是用來監聽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域對象的屬性值變化情況。