Implement a HttpSessionListener. This listener will be notified of all
sessions that are destroyed (it's sessionDestroyed() method will be
called). In that method you can check which user is logged off en remove
that user from the (applicationscope, I guess) list.
See code sample below.
Good luck,
Martijn
An example of a session listener that just counts sessions:
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SessionWatcher implements HttpSessionListener {
private static final Log log = LogFactory.getLog(HttpSessionListener.class);
private static int activeSessions = 0;
/*
* @see
javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent event) {
activeSessions++;
log.debug("Session created with id:"+event.getSession().getId()
+". No of active sessions:"+activeSessions);
System.out.println("Session created with id:"+event.getSession().getId()
+". No of active sessions:"+activeSessions);
}
/*
* @see
javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent event) {
if(activeSessions > 0) {
activeSessions--;
}
log.debug("Session destroyed with id:"+event.getSession().getId()
+". No of active sessions:"+activeSessions);
System.out.println("Session destroyed with id:"+event.getSession().getId()
+". No of active sessions:"+activeSessions);
}
}
Add something like this to your web.xml:
<listener>
<listener-class>
com.zenz.mzz.view.listeners.SessionWatcher
</listener-class>
</listener>
Oscar Picasso wrote:
Hi,
I am looking for advises on how to implement a use case that is, think, quite
common.
In my application users can log in. They can also loggout either explicitly or
because of session timeout.
In one part of my application I want to display the total number of logged
users and a list of each of these users.
My main problem is how to remove an user from the list when he is logged out
implicitly by a session timeout.
How would you solve this ?
Thanks
---------------------------------
How low will we go? Check out Yahoo! Messenger’s low PC-to-Phone call rates.
--
*Cumquat Information Technology*
De Dreef 19
3706 BR Zeist
T +31 (0)30 - 6940490
F +31 (0)10 - 6940499
http://www.cumquat.nl <http://www.cumquat.nl/>
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
M +31 6 22 384 318
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]