js| Statistics | online | Number of people online | repeat
To prevent repeat online, the number of online statistics is usually three ways: 1. Records from the database 2. Using ServletContext type Application
3. Use session.
The first approach is simpler but will put a burden on the database. The second approach has not yet been implemented.:)
This article realizes the practice of using session, the basic idea is to set up a session record landing name when normal landing, at this time excitation listener.
The code is as follows:
1. Landing on the assumption of landing name Logininfo[0], set up the session record login name.
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 landing");
Return
}else{
Session.setmaxinactiveinterval (3000); The life cycle of the session
Session.setattribute (Logininfo[0],olu);
}
2. Listener code: Onlineuser
/*
* Date Created 2005-5-12
*
Objective
*
*/
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
*
Objective
* For statistics online number, prevent unified account login, exit cancellation account
*/
public class Onlineuser implements httpsessionbindinglistener{
private static Onlineuser instance = NULL;
Synchronized public static Onlineuser getinstance ()//singleton mode ensures that there is only one Onlineuser instance
// {
if (instance = null)
// {
Instance = new Onlineuser ();
// }
return instance;
// }
static Logger 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 ("repetition");
Break
}
}
return existuser;
}
public boolean deleteuser (String userName) {
OnLineUser.logger.info ("Remove start!!");
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 ("find");
Curruserindex=i;
Break
}
}
if (curruserindex!=-1) {
OnLineUser.logger.info ("Remove:" +curruserindex);
Users.remove (Curruserindex);
Users.trimtosize ();
return true;
}
}
return false;
}
Public Vector Getonlineuser ()
{
return to users;
}
public void Valuebound (Httpsessionbindingevent e) {
Users.trimtosize ();
Users.add (E.getname ());
OnLineUser.logger.info (E.getname () + "\ t login to system \ T" + (new Date ());
OnLineUser.logger.info ("First" +i+ "entry");
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. To remember the life onlineuser before all the web.xml of the servlet,
<listener>
<listener-class>cn.wst.common.online.onlineuser </listener-class>
</listener