Java中HttpSessionAttributeListener监听器的示例
下文笔者将讲述HttpSessionAttributelistener监听器的相关示例说明,如下所示:
例
HttpSessionAttributeListener的功能: 用于对session的属性(添加,删除,替换)进行监听
HttpSessionAttributeListener中的方法
public void attributeAdded(HttpSessionBindingEvent se);//增加属性 |
public void attributeRemoved(HttpSessionBindingEvent se);//删除属性 |
public void attributereplaced(HttpSessionBindingEvent se);//替换属性 |
//HttpSessionBindingEvent事件: |
public String getName();//取得属性的名称 |
public Object getValue();//取得属性的值 |
public HttpSession getSession();//取得当前的session |
//MyAttributeListener.java package com.java265; 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("HttpSessionAttributeListener Attribute added : " + attributeName + " : " + attributeValue); } @Override public void attributeRemoved(HttpSessionBindingEvent event) { String attributeName = event.getName(); Object attributeValue = event.getValue(); System.out.println("HttpSessionAttributeListener 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.java265.MyAttributeListener</listener-class> </listener> //test.jsp <html> <body> <% session = request.getSession(); session.setAttribute("webAdd", "java265.com"); session.setAttribute("name", "java爱好者"); session.removeAttribute("name") %> </body> </html>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。