使用 HttpSessionBindingListeners 跟踪注销时间 [英] trace logout time using HttpSessionBindingListeners

查看:38
本文介绍了使用 HttpSessionBindingListeners 跟踪注销时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 httpsessionbindinglistner 跟踪注销时间?我已经给出了示例代码并在下面给出,但它不起作用.资源是这里

How can i trace log out time using httpsessionbindinglistner ? I have given sample code and giving below but it is not working. RESOURCE IS HERE

package com.tunatore.listeners;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 *
 * @author tunatore
 */
public class ObjectWillBeInSession implements HttpSessionBindingListener{

private String property1 = null;
private String property2 =null;


@Override
public void valueBound(HttpSessionBindingEvent event) {
    //code to run when ObjectWillBeInSession object associated with a http session
 }

 @Override
 public void valueUnbound(HttpSessionBindingEvent event) {
    //code to run when ObjectWillBeInSession object removed from a http session        
    //logging into a database server could be done here
 /**
  * @return the property1
  */
 public String getProperty1() {
    return property1;
 }

 /**
  * @param property1 the property1 to set
  */
  public void setProperty1(String property1) {
    this.property1 = property1;
 }

 /**
  * @return the property2
  */
 public String getProperty2() {
    return property2;
  }

  /**
  * @param property2 the property2 to set
  */
 public void setProperty2(String property2) {
    this.property2 = property2;
 }

}

logout.jsp//这里我想在浏览器关闭或会话超时发生时将注销时间插入到数据库中

logout.jsp// here i want to insert logout time into the database when browser is closed or session time out occurs

    <%
        ObjectWillBeInSession owi = new ObjectWillBeInSession();
        owi.setProperty1("I am a value for Property1");
        owi.setProperty2("I am a value for Property2");
        //this will call HttpSessionBindingListener's 
        //valueBound method for the object
        session.setAttribute("owi", owi);

        //this will call HttpSessionBindingListener's 
        //valueUnbound method for the object
        session.removeAttribute("owi");   
            //INSERT INTO DB.......BUT IT IS NOT WORKING
     %>

推荐答案

我认为您必须使用 HttpSessionListener 而不是 Binding listener.下面的示例显示了服务器中的活动会话数.

I think you have to use HttpSessionListener instead of Binding listener. Below example shows the number of active sessions in the server.

public class SessionCounter implements javax.servlet.http.HttpSessionListener{
    /**
     * Number of active sessions
     */
    private static int activeSessions = 0;

    public void sessionCreated(javax.servlet.http.HttpSessionEvent se) {
        activeSessions++;
        logSessionCount();
    }

    public void sessionDestroyed(javax.servlet.http.HttpSessionEvent se) {
        if (activeSessions > 0){
            activeSessions--;
        }
        logSessionCount();
    }
    private void logSessionCount(){
        java.lang.System.out.println("Number of active sessions : " + activeSessions);
    }
    public static int getActiveSessions() {
        return activeSessions;
    }
}

这篇关于使用 HttpSessionBindingListeners 跟踪注销时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆