使用HttpSessionBindingListeners跟踪注销时间
问题描述:
如何使用httpsessionbindinglistner跟踪注销时间?我已经给出了示例代码并在下面给出,但它不起作用。 RESOURCE IS HERE使用HttpSessionBindingListeners跟踪注销时间
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 //这里我想插入注销时间到数据库关闭浏览器时或会话超时发生
<%
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,而不是绑定监听器。以下示例显示服务器中活动会话的数量。
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;
}
}
yikes!感谢Ravindra,但是logout.jsp有没有代码错误? – Tom 2012-03-22 09:44:47
不会。注销时不会有任何错误。 – 2012-03-22 09:46:48
好吧,但[这里](http://*.com/questions/9425476/lock-logged-in-user-in-oracle-by-java)Jigar通过使用Binding Listener解释 – Tom 2012-03-22 09:54:51