package com.mkyong;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class MyAttributeListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
String attributeName = event.getName();
Object attributeValue = event.getValue();
System.out.println("Attribute added : " + attributeName + " : "
+ attributeValue);
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
String attributeName = event.getName();
Object attributeValue = event.getValue();
System.out.println("Attribute removed : " + attributeName + " : "
+ attributeValue);
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
String attributeName = event.getName();
Object attributeValue = event.getValue();
System.out.println("Attribute replaced : " + attributeName + " : "
+ attributeValue);
}
}
web.xml
<listener>
<listener-class>com.mkyong.MyAttributeListener</listener-class>
</listener>
<body>
<%
session = request.getSession();
session.setAttribute("url", "mkyong.com"); //attributeAdded() is executed
session.setAttribute("url", "mkyong2.com"); //attributeReplaced() is executed
session.removeAttribute("url"); //attributeRemoved() is executed
%>
</body>
</html>
原文: http://www.mkyong.com/servlet/a-simple-httpsessionattributelistener-example/
源代码:http://pan.baidu.com/share/link?shareid=470109&uk=3878681452