There are three methods to prevent repeated online statistics: 1. Use Database records 2. Use ServletContext-type application 3. Use session.
The first approach is relatively simple, but it will cause a burden on the database. The second approach has not been implemented yet .:)
This article implements the practice of using sessions. The basic idea is to create a session record login name during normal login, and then stimulate the listener.
The Code is as follows:
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
/*
* Creation date: 2005-5-12
*
* Purpose:
*
*/
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