servlet监听对session监听器

Servlet监听器对Session监听的方法如下:
1.HttpSessionListener
public void sessionCreated(HttpSessionEvent se):创建
public void sessionDestroyed(HttpSessionEvent se):销毁

2.HttpSessionAttributeListener
public void attributeAdded(HttpSessionBindingEvent se):增加属性
public void attributeRemoved(HttpSessionBindingEvent se):删除属性
public void attributeReplaced(HttpSessionBindingEvent se):更改属性


实例:

SessionListenerLife.java:

程序代码

package mgc.listener.test;

import javax.servlet.http.*;

public class SessionListenerLife implements HttpSessionListener,HttpSessionAttributeListener {

private HttpSession session = null ;

public void sessionCreated(HttpSessionEvent se) {

this.session = se.getSession();
System.out.println("** 创建...") ;
System.out.println("SessionID:" + this.session.getId()) ;
}

public void sessionDestroyed(HttpSessionEvent se) {

System.out.println("**销毁...") ;
}

public void attributeAdded(HttpSessionBindingEvent se) {

System.out.println("**增加属性:" + se.getName() + "-->" + se.getValue()) ;
}

public void attributeRemoved(HttpSessionBindingEvent se) {

System.out.println("**删除属性:" + se.getName() + "-->" + se.getValue()) ;
}

public void attributeReplaced(HttpSessionBindingEvent se) {

System.out.println("**更改属性:" + se.getName() + "-->" + se.getValue()) ;
}
}


web.xml:

程序代码

<listener>
<listener-class>mgc.listener.test.SessionListenerLife</listener-class>
</listener>
<session-config>
<session-timeout>1</session-timeout>
</session-config>


sessionlistener.jsp:

程序代码

<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>sessionlistener</title>
</head>

<body>
<%
//设置属性
session.setAttribute("mgc","Magci") ;
//删除属性
//session.removeAttribute("mgc") ;
//销毁
//session.invalidate() ;
%>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值