監聽域對象中屬性變動的監聽器html
域對象中屬性的變動的事件監聽器就是用來監聽ServletContext、HttpSession、HttpServletRequest這三個對象中的屬性變動信息事件的監聽器。java
這三個監聽器接口分別是ServletContextAttributeListener、HttpSessionAttributeListener和ServletRequestAttributeListener,這三個接口中都定義了三個方法來處理被監聽對象中的屬性的增長、刪除和替換的事件,同一事件在這三個接口中對應的方法名稱徹底相同,只是接受的參數類型不一樣。web
● attributeAdded方法服務器
當向被監聽對象中增長一個屬性時,web容器就調用事件監聽器的attributeAdded方法進行響應,這個方法接收一個事件類型的參數,監聽器能夠經過這個參數來得到正在增長屬性的域對象和被保存到域中的屬性對象。session
各個域屬性監聽器中的完整語法定義爲:app
public void attributeAdded(ServletContextAttributeEvent event)jsp
public void attributeAdded(HttpSessionBindingEvent event)ide
public void attributeRemove(ServletRequestAttributeEvent event)測試
● attributeRemove方法orm
當刪除被監聽對象中的一個屬性時,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域對象的屬性值變化狀況。