SVN8.COM - SVN中文技术网

投递文章 投稿指南 SVN中文技术网公告:技术交流诚聘优秀版主最新公告
搜索: 您的位置主页>JAVA技术>J2EE>演示Session Listener的使用方法

演示Session Listener的使用方法

SVN技术网 www.svn8.com 2008-08-27 12:27:01   来源:   作者:  评论:1 点击:
import java.util.Date;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionListen implements HttpSessionListener {

  private int sessionCount;

  public SessionListen() {
    this.sessionCount = 0;
  }

  public void sessionCreated(HttpSessionEvent se) {
    HttpSession session = se.getSession();
    session.setMaxInactiveInterval(60);
    synchronized (this) {
      sessionCount++;
    }
    String id = session.getId();
    Date now = new Date();
    String message = new StringBuffer("New Session created on ").append(
        now.toString()).append("nID: ").append(id).append("n")
        .append("There are now ").append("" + sessionCount).append(
            " live sessions in the application.").toString();

    System.out.println(message);
  }

  public void sessionDestroyed(HttpSessionEvent se) {

    HttpSession session = se.getSession();
    String id = session.getId();
    synchronized (this) {
      --sessionCount;
    }
    String message = new StringBuffer("Session destroyed"
        + "nValue of destroyed session ID is").append("" + id).append(
        "n").append("There are now ").append("" + sessionCount)
        .append(" live sessions in the application.").toString();
    System.out.println(message);
  }
}

技术交流 录入:SVN中文技术网[www.svn8.com]
Tags:  
责任编辑:
  • 请文明参与讨论,禁止漫骂攻击。 用户名:新注册) 密码: 匿名:
    评论总数:1 [ 查看全部 ] 网友评论
    关于我们 - 联系我们 - 广告服务 - RSS订阅 - 网站地图 - 返回顶部