session - Tracking user activity in Java EE -
i track user's sessions. interested in getting user logname, context accessed , time when accessed context.
i thinking of using class implements httpsessionlistener (overriding sessioncreated(final httpsessionevent se) , sessiondestroyed(final httpsessionevent se)) on these methods don't access request (from pull user's logname , context accessed).
any suggestions welcome.
i think servlet filter more suitable want. suggest write custom filter around urls want track.
in dofilter()
method have access httpservletrequest
needed. request object can httpsession
too.
here example:
@webfilter("/*") public class trackingfilter implements filter { private filterconfig filterconfig; @override public void init(filterconfig config) throws servletexception { this.filterconfig = config; } @override public void dofilter(servletrequest req, servletresponse res, filterchain chain) throws ioexception, servletexception { httpservletrequest request = (httpservletrequest) req; httpsession session = request.getsession(false); string loggedinuser = "unregistered user"; //assuming have session attribute named user username if(session != null && session.getattribute("user") != null) { loggedinuser = (string) session.getattribute("user"); } date accesseddate = new date(); filterconfig.getservletcontext().log( string.format("%s accessed context %s on %tf %tt", loggedinuser, request.getrequesturi() , accesseddate, accesseddate) ); chain.dofilter(req, res); } @override public void destroy() { } }
see also: javaee6 tutorial section filters.
Comments
Post a Comment