httpsession - How to find out what open sessions my servlet based application is handling at any given moment -
i need write servlet that, when called, gets information list of opened sessions.
is there way this?
implement httpsessionlistener
, give static set<httpsession>
property, add session during sessioncreated()
method, remove session during sessiondestroyed()
method, register listener <listener>
in web.xml
. you've class has open sessions in current jboss instance collected. here's basic example:
public httpsessioncollector implements httpsessionlistener { private static final set<httpsession> sessions = concurrenthashmap.newkeyset(); public void sessioncreated(httpsessionevent event) { sessions.add(event.getsession()); } public void sessiondestroyed(httpsessionevent event) { sessions.remove(event.getsession()); } public static set<httpsession> getsessions() { return sessions; } }
then in servlet do:
set<httpsession> sessions = httpsessioncollector.getsessions();
if rather want store/get in application scope can make set<httpsession>
non-static, let httpsessioncollector
implement servletcontextlistener
as well , add following methods:
public void contextcreated(servletcontextevent event) { event.getservletcontext().setattribute("httpsessioncollector.instance", this); } public static httpsessioncollector getcurrentinstance(servletcontext context) { return (httpsessioncollector) context.getattribute("httpsessioncollector.instance"); }
which can use in servlet follows:
httpsessioncollector collector = httpsessioncollector.getcurrentinstance(getservletcontext()); set<httpsession> sessions = collector.getsessions();
Comments
Post a Comment