Page online access count & online logon count 1

Source: Internet
Author: User

I. Online page access count statistics

Onlinesessionlistener listener (httpsessionlistener) interface to achieve online page access statistics. When a user accesses the page, a session is created, and the public void sessioncreated (httpsessionevent SE) method is triggered, if the session fails, the public void sessiondestroyed (httpsessionevent SE) method is triggered. In this method, we use onlinelist to measure the number of online users.

public class OnlineList {private static final OnlineList onlineList = new OnlineList();private int maxSession;private int activeSession;private OnlineList()    {           }    public static OnlineList getInstance()    {        return onlineList;    }        public void addSession(){    activeSession++;    if(activeSession>=maxSession){    maxSession=activeSession;    }}        public void delSession(){    if(activeSession   >   0)        activeSession--;      }   public  int getActiveSession(){return activeSession;}public  int getmaxSession(){  return maxSession; }  }

  

public class OnlineSessionListener implements HttpSessionListener {private OnlineList ol = OnlineList.getInstance();public void sessionCreated(HttpSessionEvent se) {ol.addSession();}public void sessionDestroyed(HttpSessionEvent se) { ol.delSession();}}

Ii. Online login count statistics  

The userlistener listener (implementing the httpsessionattributelistener Interface) is used to calculate the number of online logon users. When a user is added to the session, the listener calls the public void valuebound (httpsessionbindingevent event) method. If a user exits the system, the listener calls the public void valueunbound (httpsessionbindingevent event) method. We use userlist to collect user information.

In the design of the userlist class, we applied Singleton design patterns. For more information about the design patterns, see related books. Userlist is a singleton class. The so-called Singleton class refers to a class that has only one instance and is self-instantiated and provided to the entire system. One of the most important features of a singleton class is that the class constructor is private, which avoids the external use of this class constructor to directly create multiple instances. In line 2 of the Code, define a static constant userlist, which represents an object of the userlist class. This object is generated when the userlist class is loaded. 11th ~ Line 14 declares that the userlist class constructor is private to avoid using the userlist class constructor to create its object externally. Note that if no constructor is written in the class, the Java compiler will provide a default public constructor without parameters for this class. In this way, you can create an object through the class constructor externally, so userlist is no longer a singleton class. Since the construction method of the userlist class is private, the object cannot be constructed using new outside, so in the Code 16th ~ Row 19 defines a static method getinstance (). In this method, the userlist class object created during class loading is returned. Because the getinstance () method is static, it can be called directly by class name.
So why should we design userlist as a singleton class? This is because the userlist object is used to store and obtain the list of online users, and this user list should be the same for all pages, therefore, the userlist class is designed as a singleton class. In this way, all classes access the same userlist object.
Line 2 of the Code defines a private vector variable. In the userlist constructor, V is initialized to store string objects. Note that arraylist is not used here, because the userlist object may be accessed by multiple threads at the same time, because arraylist is not synchronized, while vector is synchronized, therefore, vector is used to save the user list.

Import javax. servlet. HTTP. httpsessionattributelistener; import javax. servlet. HTTP. httpsessionbindingevent; public class userlistener implements httpsessionattributelistener {// User Logon ID card private string username; private userlist U1 = userlist. getinstance (); // determines whether the user has a public Boolean isexist (string sfz) throws exception {try {return u1.isexist (sfz);} catch (exception ex) {ex. printstacktrace (); Return false ;}} Public String GetUserName () {return username;} public void setusername (string username) {username = username;} public void attributeadded (httpsessionbindingevent) event) {try {If ("username ". equals (event. getname () {u1.adduser (string) event. getvalue () ;}} catch (exception e) {e. printstacktrace () ;}} public void attributeremoved (httpsessionbindingevent event) {try {If ("username ". equals (event. getname () {u1.removeuser (string) event. getvalue () ;}} catch (exception e) {e. printstacktrace () ;}} public void attributereplaced (httpsessionbindingevent arg0) {// todo auto-generated method stub }}

  

Import Java. util. enumeration; import Java. util. iterator; import Java. util. vector; public class userlist {Private Static final userlist = new userlist (); Private vector v = new vector (); Private int maxuser; private userlist () {// v = new vector ();} public static userlist getinstance () {return userlist;} // Save the User Login ID card to public void adduser (string sfz) in the vector) throws exception {try {If (sfz! = NULL &&! "". Equals (sfz) {If (v. indexof (sfz)> = 0) // determines whether a return exists. // possible operations // Add a logon ID v. addelement (sfz); If (getusercount ()> maxuser) {maxuser = getusercount () ;}} catch (exception ex) {ex. printstacktrace ();} finally {} public Boolean isexist (string sfz) throws exception {try {If (v. indexof (sfz)> = 0) return true; return false;} catch (exception ex) {ex. printstacktrace (); Return false ;}// Delete the user logon ID Public void removeuser (string sfz) throws exception {try {If (sfz! = NULL &&! "". Equals (sfz) {// modify the database // remove the user logon ID v. removeelement (sfz) ;}} catch (exception ex) {ex. printstacktrace (); // write the log} finally {} // returns the vector enumeration public enumeration getuserlist () {return v. elements ();} // return iterator public iterator getuserlistiparts () {return v. iterator ();} // returns the number of online users public int getusercount () {return v. size () ;}// return the peak number of online users public int getmaxuser () {return maxuser ;}}

  

Add the following information to the Web-XML file:

<Listener>

<Listener-class> com. myxmu. listener. userlistener </listener-class>

</Listener>

 

The online count statistics program has some problems. If the user does not exit the logon and closes the browser directly, the user still exists in the session on the server end until the session timeout value occurs. Therefore, statistics on the number of online users can only calculate the approximate number of online users within a period of time, rather than the exact number of people. To improve statistical accuracy, you can set a script on the client. When the browser is closed, a request is automatically sent to the server. After the server receives the request, the session becomes invalid. However, this cannot be 100% accurate, because the client's browser may end abnormally or the client's machine may crash.

 

Reprinted from ---------------- http://blog.sina.com.cn/s/blog_6e5988eb01013iva.html

Page online access count & online logon count 1

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.