Java determines the current number of users and the number of users currently logged on-session principle of tool class

Source: Internet
Author: User
Tags sessions

Javaweb development, sometimes encountered statistics or management user login number or current online how many users, respectively, who is the case. Of course, there are many ways to achieve. The following is a list of statistics that are implemented by the session.

 Public classMysessionlistenerImplementsHttpsessionlistener {/*** Create session*/@SuppressWarnings ({"Rawtypes", "unchecked" })    Public voidsessioncreated (Httpsessionevent event) {HttpSession session=event.getsession (); ServletContext Application=Session.getservletcontext (); //Save all sessions in the application range by a hashset setHashSet sessions = (HashSet) application.getattribute ("Sessions"); if(Sessions = =NULL) {Sessions=NewHashSet (); Application.setattribute ("Sessions", sessions); }            //the newly created session is added to the HashSet setSessions.add (session); //Sessions collection can be removed from the application range elsewhere//then use Sessions.size () to get the number of sessions currently active, which is "online people"   }    /*** Destroy Session*/@SuppressWarnings ("Rawtypes")    Public voidsessiondestroyed (Httpsessionevent event) {HttpSession session=event.getsession (); ServletContext Application=Session.getservletcontext (); HashSet Sessions= (HashSet) application.getattribute ("Sessions"); //the destroyed session is removed from the hashset set.Sessions.remove (session); }      /*** Access to the number of online users and number of users logged in *@paramrequest current Requested Object *@paramflag=1 Current Online users flag=2 number of currently logged on users *@return    */@SuppressWarnings ("Rawtypes")    Public Static intGetmaxuser (HttpServletRequest request,intflag) {HttpSession session=request.getsession (); ServletContext Application=Session.getservletcontext (); HashSet Sessions= (HashSet) application.getattribute ("Sessions"); intCountuser =sessions.size ();//System.out.println ("################## #当前在线人数:" +sessions.size ());//String onlineuser = "";        intCountonlineuser = 0;  for(Iterator it =(Iterator) sessions.iterator (); It.hasnext ();) {HttpSession se=(HttpSession) it.next (); if(se!=NULL) {UserInfo UI= (UserInfo) se.getattribute ("User_info"); if(ui!=NULL){//Onlineuser + = "___" +ui.getname ();countonlineuser++; }            }        }//System.out.println ("################## #当前登录用户:" +onlineuser);        returnFlag==1?Countuser:countonlineuser; } 

Java determines the current number of users and the number of users currently logged on-session principle of tool class

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.