HttpSessionAttributeListener的例子

来源:互联网 发布:抑郁和悲伤的区别 知乎 编辑:程序博客网 时间:2024/05/22 03:37
package com.mkyong;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;public class MyAttributeListener implements HttpSessionAttributeListener {@Overridepublic void attributeAdded(HttpSessionBindingEvent event) {String attributeName = event.getName();Object attributeValue = event.getValue();System.out.println("Attribute added : " + attributeName + " : "+ attributeValue);}@Overridepublic void attributeRemoved(HttpSessionBindingEvent event) {String attributeName = event.getName();Object attributeValue = event.getValue();System.out.println("Attribute removed : " + attributeName + " : "+ attributeValue);}@Overridepublic 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 executedsession.setAttribute("url", "mkyong2.com"); //attributeReplaced() is executedsession.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