Session listener, used to count the number of online users

Source: Internet
Author: User

1. Assume that logininfo [0] is the login name and the session name is created.

If (rs_login.next ()){
Login_servlet.logger.info ("login verification successful"); // log4j
Onlineuser Olu = new onlineuser ();//
If (! Olu. existuser (logininfo [0]) {
Login_servlet.logger.info ("Repeated login ");
Return;

} Else {
Session. setmaxinactiveinterval (3000); // the lifecycle of the session
Session. setattribute (logininfo [0], Olu );


}

2. Listener code: onlineuser

Package CN. WST. Common. online;

Import javax. servlet. http. httpsessionbindingevent;
Import javax. servlet. http. httpsessionbindinglistener;

Import org. Apache. log4j. Logger;

Import java. util .*;
/**
* @ Author wangshangting
*
* Purpose:
* Used to count the number of online users, prevent uniform account logon and exit account cancellation
*/
Public class onlineuser implements httpsessionbindinglistener {
// Private Static onlineuser instance = NULL;

// Synchronized public static onlineuser getinstance () // Singleton mode, ensure that there is only one onlineuser instance
//{
// If (instance = NULL)
//{
// Instance = new onlineuser ();
//}
// Return instance;
//}



Static logger = logger. getlogger (onlineuser. Class. getname ());
Public onlineuser (){
}

Private Static vector users = new vector (); // static variable !!!!!!!!!!!!
Private int I = 1;
Public int getcount (){

Users. trimtosize ();

Return users. Capacity ();
}
Public Boolean existuser (string username ){
Users. trimtosize ();
Boolean existuser = true;
For (INT I = 0; I <users. Capacity (); I ++)
{
If (username. Equals (string) users. Get (I )))
{
Existuser = false;
Onlineuser.logger.info (users. Get (I ));
Onlineuser.logger.info ("repeated ");
Break;
}
}
Return existuser;
}


Public Boolean deleteuser (string username ){
Onlineuser.logger.info ("start to remove !! ");
Users. trimtosize ();

If (! Existuser (username )){
Int curruserindex =-1;
For (INT I = 0; I <users. Capacity (); I ++ ){
If (username. Equals (string) users. Get (I ))){
// Onlineuser.logger.info ("found ");
Curruserindex = I;
Break;
}
}
If (curruserindex! =-1 ){
Onlineuser.logger.info ("Remove:" + curruserindex );
Users. Remove (curruserindex );
Users. trimtosize ();
Return true;
}

}
Return false;
}

Public vector getonlineuser ()
{
Return users;
}

Public void valuebound (httpsessionbindingevent e ){
Users. trimtosize ();

Users. Add (E. getname ());


Onlineuser.logger.info (E. getname () + "/T log on to the system/t" + (new date ()));
Onlineuser.logger.info ("+" + I + "");
I ++;

For (INT I = 0; I <users. Size (); I ++ ){
Onlineuser.logger.info ("" + I + ":" + users. Get (I ));
}

System. Out. println ("number of online users:" + getcount ());

}
 


/**
*
*/
Public void valueunbound (httpsessionbindingevent e ){
Users. trimtosize ();
String username = E. getname ();

Deleteuser (username );

Onlineuser.logger.info (username + "/T exit system/t" + (new date ()));
Onlineuser.logger.info ("number of online users:" + getcount ());
}
}

3. Remember to live onlineuser before all servlets in Web. xml,

<Listener>
<Listener-class> CN. WST. Common. Online. onlineuser </listener-class>
</Listener>

Article Source: http://www.theserverside.com/discussions/thread.tss? Thread_id = 34076

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.