In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to count the current online number of java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.
Java counts the current number of online users
Package com.hnedu.stat
/ * *
* calculate the number of people online, indicating:
*
* put in the visit page:
* com.hnedu.stat.OnlineCounter.addUser (object object)
* ip is generally used as the input value of object. Of course, if the website counts the number of people who log in online, you can also use a user name.
* to input values as object.
*
* call to display the number of people online
* com.hnedu.stat.OnlineCounter.getOnlineCount () *
*
* com.hnedu.stat.OnlineCounter.getOnline ()
* return a List that stores the passed object value and the last time the page was visited in the form of String [].
* by crazyren 2003-08-05 eMail: hotmail.com "> c_crazyren@hotmail.com
, /
Import Java.util.List
Import java.util.ArrayList
Import javax.servlet.http.HttpSession
Public class OnlineCounter {
Private static List 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 users'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 extracted from the list.
String [] temp = new String [2]
/ / cyclic list
For (int I = 0; I
< list.size(); i++) { temp = (String[])list.get(i); //如果从list里面取出的用户ip和str里面存放的ip相同,则更新访问时间 if(temp[0].equals(str[0])) { list.set(i,str); return; } //如果用户超过5分钟没有访问,则认为不在线 if(subTime(nowtime,temp[1]) >300) {
List.remove (I)
}
}
/ / add a new online user
List.add (str)
/ / release resources
Str = null;temp = null
}
Public static int getOnlineCount () {
/ / return the current number of online users
Return list.size ()
}
Public static List getOnline () {
Return list
}
/ * * calculate two time differences and return the difference in seconds * /
Private static int subTime (String src,String des) {
Int n = 0
Java.util.Calendar ca = java.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
}
}
The two methods in the referenced StringUtils:
/ * *
* get the current time
Time format of * @ param parrten output
* @ return 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 equality, > 0 T1 > T2
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.