Method 1:
Write a singleton class. When someone logs in, the total number is increased by 1. When someone logs out, the total number is reduced by 1.
Public class accountsingle {
Private Static accountsingle account = new accountsingle ();
Private int Total = 0;
Private accountsingle (){}
// Obtain the unique instance object
Public static accountsingle getinstance (){
Return account;
}
// Login operation. The total number is increased by 1. synchronization is required.
Public synchronized void loginaction (){
Total + = 1;
}
// Logout operation. The total number is reduced by 1. synchronization is required.
Public synchronized void logoutaction (){
Total-= 1;
}
Public int gettotal (){
Return total;
}
}
This class can be used during login/logout.
Add a line to the login code: accountsingle. getinstance (). loginaction ();
The logout operation is similar.
This is a simple method. You can also add a hashtable in a single instance to save the information of each relay user.
You may want to ask: A lot of people close the page directly before exiting the system, without clicking your Logout button, this problem can be solved through
JS. Make a declaration in the page Body Tag: onUnload = "logout ()"
Execute the logout operation in the logout () function.
If you think this is not safe, you can write a listening thread to listen to the session.
Thanks to wwwer1
Method 2: Pass the session
1. Implementation Interface Class
Public class sessionlistener implements httpsessionattributelistener {
Public void attributeadded (httpsessionbindingevent event ){
If (event. getname (). Equals ("userid ")){
Try
{
Onlinecounter. douser (event. getvalue (), false );
} Catch (exception E)
{
E. printstacktrace ();
}
}
}
Public void attributeremoved (httpsessionbindingevent event ){
If (event. getname (). Equals ("userid ")){
Try {
Onlinecounter. douser (event. getvalue (), true );
} Catch (exception e ){
E. printstacktrace ();
}
}
}
Public void attributereplaced (httpsessionbindingevent event ){}
}
2. onlinecounter
Public class onlinecounter {
Private Static list = new arraylist ();
Private Static list list1 = new arraylist ();
Private onlinecounter (){}
// Count the current number of online members
Public static void douser (Object object, Boolean BL ){
// Obtain the current user access time
String nowtime = gettime ("mm: SS ");
// The STR array is used to store the user ID and access time.
String [] STR = new string [2];
STR [0] = object. tostring ();
STR [1] = nowtime;
// The temp array is used to temporarily store the user ID and access time retrieved from the list.
String [] temp = new string [2];
// Cyclic list
For (INT I = 0; I <list. Size (); I ++ ){
Temp = (string []) list. Get (I );
// If the user IP address retrieved from the list is the same as the ID in STR, the access time is updated.
If (BL = false & temp [0]. Equals (STR [0]) {
List. Set (I, STR );
Return;
}
// If BL = true is passed in the listener class, the listener is considered offline.
If (BL = true & temp [0]. Equals (STR [0]) {
List. Remove (I );
}
}
// Add a new online user
If (BL = false) List. Add (STR );
// Release resources
STR = NULL; temp = NULL;
}
// Count all online users of the website
Public static void alluser (Object object ){
// Obtain the current user access time
String nowtime = gettime ("mm: SS ");
// The STR array is used to store the user's IP address and access time.
String [] STR = new string [2];
STR [0] = object. tostring ();
STR [1] = nowtime;
// The temp array is used to temporarily store the user IP address and access time retrieved from the list.
String [] temp = new string [2];
// Cyclic list
For (INT I = 0; I <list1.size (); I ++ ){
Temp = (string []) list1.get (I );
// If the user IP address retrieved from the list is the same as the IP address stored in STR, the access time is updated.
If (temp [0]. Equals (STR [0]) {
List1.set (I, STR );
Return;
}
// If the user has not accessed the service for more than 10 minutes, the service is considered offline.
If (subtime (nowtime, temp [1])> 600 ){
// System. Out. Print ("clearing expired list values" + I );
List1.remove (I );
}
}
// Add a new online user
List1.add (STR );
// Release resources
STR = NULL; temp = NULL;
}
Public static int getonlineallcount (){
// Returns the current number of online users.
Return list1.size ();
}
Public static int getonlinecount (){
// Returns the current number of online users.
Return list. Size ();
}
/*
* Public static void romovelist ()
*{
*}
*/
Public static list getonline (){
Return list;
}
/** Calculate the time difference and return the time difference */
Public static int subtime (string SRC, string des ){
Int n = 0;
Java. util. Calendar CA = java. util. Calendar. getinstance ();
Long time1 = comparestringtime (SRC, Des, "Mm: SS ");
CA. settimeinmillis (time1 );
N = (ca. Get (Java. util. Calendar. Minute) * 60;
N = N + Ca. Get (Java. util. Calendar. Second );
Return N;
}
/**
*
* Obtain the current time
*
* @ Param parrten
* Output Time Format
*
* @ Return time
*
*/
Public static string gettime (string parrten ){
String timestr;
If (parrten = NULL | parrten. Equals ("")){
Parrten = "yyyymmddhhmmss ";
}
Java. Text. simpledateformat SDF = new simpledateformat (parrten );
Java. util. Date CDAY = new date ();
Timestr = SDF. Format (CDAY );
Return timestr;
}
/**
* Compare the time of two strings
* @ Param T1
* Time 1
* @ Param T2
* Time 2
* @ Param parrten
* Time Format: yyyy-mm-dd
* @ Return returns long = 0 equal,> 0 T1> T2, <0 t1 <t2
*/
Public static long comparestringtime (string T1, string T2, string parrten ){
Simpledateformat formatter = new simpledateformat (parrten );
Parseposition Pos = new parseposition (0 );
Parseposition pos1 = new parseposition (0 );
Date dt1 = formatter. parse (T1, POS );
Date dt2 = formatter. parse (t2, pos1 );
Long L = dt1.gettime ()-dt2.gettime ();
Return L;
}
}
3. on the web. Configure the listener class in XML
<! -- Online user -->
<Listener>
<Listener-class> baby.com. Common. sessionlistener </listener-class>
</Listener>
4. jsp page
String IP = "";
If (request. getheader ("X-forwarded-for") = NULL ){
IP = request. getremoteaddr ();
} Else {
IP = request. getheader ("X-forwarded-");
}
Onlinecounter. alluser (IP );