Calculate online numbers

Source: Internet
Author: User
Tags object object

Solution Flow Statistics v3.0, 2006-6-15, 3.24 MB, 107. • Records the total number of visits, total IP access, columns, daily, each page access information. Visitor is from which website arrives this website, if is through the search engine, uses is which keyword. What pages have visitors visited, how long they stayed on each page, the number of people currently online
Http://www.codefans.com/CodeList/Catalog_32_CodeTime_Desc_1.html


Package com.hnedu.stat;
/**
* Calculate the number of people online, description:
*
* In the access page put:
* Com.hnedu.stat.OnlineCounter.addUser (Object object)
* Generally use IP as the incoming value of object, of course, if the site statistics landing online number, also available user name
* To pass the value as object.
*
* Call Show Number online
* Com.hnedu.stat.OnlineCounter.getOnlineCount () *
*
* Com.hnedu.stat.OnlineCounter.getOnline ()
* Returns a list containing the incoming object value and the last access page time in string[form.
* by Crazyren 2003-08-05 email:c_crazyren@hotmail.com
*/
Import wuhuif.util.List;
Import wuhuif.util.ArrayList;
Import javax.servlet.http.HttpSession;
public class Onlinecounter {
private static list = new ArrayList ();

Private Onlinecounter () {
}
public static void AddUser (Object object) {
Get the user's current access time
String nowtime = Com.hnedu.util.StringUtils.getTime ("Mm:ss");
The str array is used to store the user's IP 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 and access time removed from the list
string[] temp = new string[2];

   //Loop list
    for (int i = 0; i < list.size (); i++) {
   &nbs p;  temp = (string[]) list.get (i);
     //If the user IP from the list is the same as the IP stored in STR, update access time
      if ( Temp[0].equals (Str[0])) {
        list.set (I,STR);
        return;
     }
     //If the user does not have access for more than 5 minutes, it is not considered online
      if (Subtime nowtime, TEMP[1] >) {
        list.remove (i);
     }
   }
   //Add a new online user
    List.add (str);
   //Free resources
    str = null;temp = null;
 }

public static int Getonlinecount () {
Back to current number online
return List.size ();
}

public static List Getonline () {
return list;
}
/** calculates two time difference and returns the number of seconds.
private static int Subtime (String src,string des) {
int n = 0;
Wuhuif.util.Calendar CA = Wuhuif.util.Calendar.getInstance ();
Long time1 =
Com.hnedu.util.StringUtils.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;
}
}

There are two ways to quote StringUtils:

 /**
   * Get current time
   * @param parrten output Time format
   * @return return time
    */
  public static string GetTime (String parrten)
  {
    String timestr
    if (parrten==null| | Parrten.equals (""))
    {
      parrten= "YYYYMMDDHHMMSS";
   }
    wuhuif.text.SimpleDateFormat sdf=new SimpleDateFormat (parrten);
    wuhuif.util.Date cday=new Date ();
    Timestr=sdf.format (cday);
    return timestr;
 }

 /**
   * Compare two string time
   * @param t1 time 1
   * @param t2 time 2
 &nb Sp * @param parrten Time format: YYYY-MM-DD
   * @return returns long = 0 equal, >0 t1>t2,<0 t1<t2
   */< br>  public static Long Comparestringtime (String t1,string t2,string parrten)
  {
   & nbsp;  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;
 }
 

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.