简体   繁体   English

使用HttpSessionBindingListeners跟踪注销时间

[英]trace logout time using HttpSessionBindingListeners

How can i trace log out time using httpsessionbindinglistner ? 如何使用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// here i want to insert logout time into the database when browser is closed or session time out occurs logout.jsp //在这里,我想在关闭浏览器或发生会话超时时将注销时间插入数据库中

    <%
        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
     %>

I think you have to use HttpSessionListener instead of Binding listener. 我认为您必须使用HttpSessionListener而不是Binding监听器。 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM