|
真爱的事业和真正的爱情一生只有一次,都值得我们温柔地相待,因为那种感觉是永远都无法复制的, 这世界真正属于你的东西其实并不多,你不好好珍惜,它便会离你而去,包括机遇,包括爱情,包括生命。 不要找任何理由, 当幸福在你身边的时候就抓住它,你就一定会很幸福! |
时 间 记 忆 |
« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
|
blog名称:玻璃杯中的花生壳 日志总数:162 评论数量:249 留言数量:1 访问次数:824717 建立时间:2004年11月4日 |
 | | |
|
|
HttpSessionListener用法- -
继上次说到Listener的功效,这里就不得不说说另外一个接口HttpSessionListener了。
上次说到了ServletContextListener,它是用来监听Servlet Context的创建和销毁的状态。今天特别有兴趣在
聊聊另一个监听的接口,名字叫做HttpSessionListener。
在理解这个接口之前,先提出一个问题,就是假设我的web应用上想知道到底有多少用户在使用?
首先看一段代码。
package demo.listener;
import javax.servlet.ServletContext;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;
public class SessionCounter implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { ServletContext ctx = event.getSession( ).getServletContext( ); Integer numSessions = (Integer) ctx.getAttribute("numSessions"); if (numSessions == null) { numSessions = new Integer(1); } else { int count = numSessions.intValue( ); numSessions = new Integer(count + 1); } ctx.setAttribute("numSessions", numSessions); } public void sessionDestroyed(HttpSessionEvent event) { ServletContext ctx = event.getSession( ).getServletContext( ); Integer numSessions = (Integer) ctx.getAttribute("numSessions"); if (numSessions == null) { numSessions = new Integer(0); } else { int count = numSessions.intValue( ); numSessions = new Integer(count - 1); } ctx.setAttribute("numSessions", numSessions); }}
在这个解决方案中,任何一个Session被创建或者销毁时,都会通知SessionCounter 这个类,当然通知的原因是必须在web.xml文件中做相关的配置工作。如下面的配置代码:
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Struts Examples</display-name> <listener> <listener-class>demo.listener.SessionCounter </listener-class> </listener> ... rest of web.xml
http://spaces.msn.com/members |
|
|
回复:HttpSessionListener用法 |
[ 2008/9/3 15:38:21 | By: Yvon(游客) ] |
| » 1 »
| | |
|