Java Web Listener Statistics online users and numbers

Source: Internet
Author: User

Online users use Httpsessionlistener listener statistics

Every time a session is established online number of users +1

Whenever a session is destroyed number of online users-1


Using the Servletrequestlistener listener to count user information

Whenever a request is established to put the current user into a collection

Remove the current user from the collection whenever the session is destroyed


I am using the Servlet3.0 listener to use annotations directly @weblistener can not be laid out in Web. xml

Configuring the Session-timeout label flag in Web. XML the session expires at 1 minutes and 0 is not expired

  <session-config>  <session-timeout>1</session-timeout>  </session-config>


servlet3.0 Conditions of Use



Operation Result:


The last wait time in a GIF image is not necessarily the same as the set 1 minutes due to the session closing time.

User entity class

Package com.scx.entity;/** * User entity class * */public class User {//Current user's session idprivate String sessionid;//Current user's IP address private Str ing ip;//the current user's first access time private String firsttime;public user () {super ();} Public String GetIP () {return IP;} public void setIp (String IP) {this.ip = IP;} Public String Getfirsttime () {return firsttime;} public void Setfirsttime (String firsttime) {this.firsttime = Firsttime;} Public String GetSessionID () {return sessionId;} public void Setsessionid (String sessionId) {This.sessionid = SessionId;}}

Httpsessionlistener Listener

Package Com.scx.listener;import Java.util.arraylist;import Javax.servlet.annotation.weblistener;import Javax.servlet.http.httpsessionevent;import Javax.servlet.http.httpsessionlistener;import Com.scx.entity.User; Import com.scx.util.sessionutil;/** * Httpsessionlistener listener * */@WebListenerpublic class Myhttpsessionlistener Implements httpsessionlistener{//current number of users private int usercounts=0; @Overridepublic void sessioncreated (httpsessionevent SE) {//sessioncreated number of users +1usercounts++;//re-save Usercountsse.getsession () in ServletContext. Getservletcontext (). SetAttribute ("Usercounts", usercounts);} @Overridepublic void sessiondestroyed (httpsessionevent se) {//sessiondestroyed number of users -1usercounts--;// Re-save Usercountsse.getsession (). Getservletcontext (). SetAttribute ("Usercounts", usercounts) in ServletContext; @ Suppresswarnings ("unchecked") arraylist<user> userlist= (arraylist<user>) se.getSession (). Getservletcontext (). getattribute ("userlist"); String sessionid=se.getsession (). GetId ();//If the current user is in UserList  Remove the current user from Userlistif (Sessionutil.getuserbysessionid (userlist, sessionId)!=null) {Userlist.remove () when the session is destroyed Sessionutil.getuserbysessionid (UserList, SessionId));} Resave the UserList collection to Servletcontextse.getsession (). Getservletcontext (). SetAttribute ("UserList", UserList);}}


Servletrequestlistener Listener

Package Com.scx.listener;import Java.text.simpledateformat;import Java.util.arraylist;import java.util.Date;import Javax.servlet.servletrequestevent;import Javax.servlet.servletrequestlistener;import Javax.servlet.annotation.weblistener;import Javax.servlet.http.httpservletrequest;import Com.scx.entity.User; Import com.scx.util.sessionutil;/** * Servletrequestlistener listener * */@WebListenerpublic class Myservletrequestlistener Implements Servletrequestlistener {//user collection private arraylist<user> userlist; @Overridepublic void Requestdestroyed (Servletrequestevent arg0) {} @SuppressWarnings ("Unchecked") @Overridepublic void requestinitialized (Servletrequestevent arg0) {//userlistuserlist= (arraylist<user>) Arg0.getservletcontext () obtained from ServletContext. getattribute ("UserList") ;//If there is no UserList object initialization in ServletContext userlistif (userlist==null) {userlist=new arraylist<user> ();} HttpServletRequest request= (HttpServletRequest) arg0.getservletrequest ();//sessionidstring sessionId= Request.getsessiOn (). GetId ()//If the current User object is created in the collection if the current sessionId does not exist (Sessionutil.getuserbysessionid (Userlist,sessionid) ==null) {User User=new User (); User.setsessionid (sessionId); User.setip (Request.getremoteaddr ()); SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd hh:MM:ss"); User.setfirsttime (Sdf.format (New Date ())); Userlist.add (user);} Save the UserList collection to Servletcontextarg0.getservletcontext (). SetAttribute ("UserList", UserList);}}

Tool class

Package Com.scx.util;import Java.util.arraylist;import Com.scx.entity.user;public class Sessionutil {// SessionID determines whether the current user exists in the collection  if there is a return to the current user  otherwise nullpublic static user Getuserbysessionid (arraylist<user> Userlist,string sessionId) {for (User user:userlist) {if (Sessionid.equals (User.getsessionid ())) {return User;}} return null;}}


Java Web Listener Statistics online users and numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.