Session-based online user statistical analysis in JSP

Source: Internet
Author: User

As a rising star, JSP plays a certain role in the Server programming environment and is closely related to its good support of a series of industry standards. Session is one of the infrastructure it provides. As a programmer, you can easily implement simple session-based user management without worrying about how the client is implemented. There are several different processing methods for online users.

One is that page refresh is controlled by the user, and the server controls a timeout time, for example, 30 minutes. After the time, the user is kicked out without any action. The advantage of this method is that if the user forgets to quit, it can prevent malicious operations by others. The disadvantage is that if you are doing a very time-consuming task that exceeds this time limit, you may have to log on again when submit. If the original leaves are forced to expire, your work may be lost. From the implementation perspective, this is the simplest, and the default implementation on the Server side is this mode.

Another way is that the site uses a Frame structure, and a Frame or hidden iframe is constantly refreshing, so that you will never be kicked out, but the server is trying to determine whether you are online, you need to set a daze time. If you have not refreshed any other leaves except the automatically refreshed ones, you will think that you are no longer online. The typical method is xici.net. Its advantage is that it can use constant refreshing to implement some functions similar to server-push, such as sending messages between netizens.

No matter which mode, some additional work is required to browse all online users. The servlet API does not obtain the Session List API.

The Listener. Servlet 2.2 and 2.3 specifications are slightly different here. HttpSessionBindingListener in 2.2 can notify you of classes when the Attribute in an HTTPSession changes. HttpSessionAttributeListener is also introduced in 2.3. since the environments I use are Visual age for java 4 and JRun server 3.1, they do not directly support Servlet 2.3 programming. Here I use HttpSessionBindingListener.

What needs to be done includes creating a new class to implement the HttpSessionBindingListener interface. This interface has two methods:

 
 
  1. public void valueBound(HttpSessionBindingEvent event)  
  2. public void valueUnbound(HttpSessionBindingEvent event) 

When you execute Session. when addattriund (String, Object) is used, if you have added a class that implements the HttpSessionBindingListener interface as Attribute, the Session will notify you of the class and call your valueBound method. On the contrary, the Session. removeAttribute method corresponds to the valueUndound method.

 
 
  1. public class HttpSessionBinding implements javax.servlet.
    http.HttpSessionBindingListener     
  2. {     
  3. ServletContext application = null;      
  4. public HttpSessionBinding(ServletContext application)     
  5. {    
  6. super();    
  7. if (application ==null)     
  8. throw new IllegalArgumentException("Null application is not accept.");    
  9. this.application = application;     
  10. }      
  11. public void valueBound(javax.servlet.http.HttpSessionBindingEvent e)    
  12. {     
  13. Vector activeSessions = (Vector) application.getAttribute
    ("activeSessions");    
  14. if (activeSessions == null)    
  15. {     
  16. activeSessions = new Vector();    
  17. }     
  18. JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");   
  19. if (sessionUser != null)    
  20. {     
  21. activeSessions.add(e.getSession());    
  22. }    
  23. application.setAttribute("activeSessions",activeSessions);     
  24. }      
  25. public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent e)      
  26. {    
  27. JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");   
  28. if (sessionUser == null)    
  29. {     
  30. Vector activeSessions = (Vector) application.getAttribute
    ("activeSessions");     
  31. if (activeSessions != null)     
  32. {    
  33. activeSessions.remove(e.getSession().getId());    
  34. application.setAttribute("activeSessions",activeSessions);     
  35. }    
  36. }     
  37. }    
  38. }  
 
 

    Assume that the JDBCUser class is an arbitrary User class. During User logon, add both the User class and HttpSessionBinding class to the Session.

    In this way, each time a user logs on, a record will be added to the vector attribute "activeSessions" in the application. Whenever the session times out, valueUnbound is triggered. In this vector, delete the session to be timed out.

     
     
    1. public void login()    
    2. throws ACLException,SQLException,IOException    
    3. {     
    4. /* get JDBC User Class */     
    5. if (user != null)     
    6. {    
    7. logout();     
    8. }     
    9. {    
    10. // if session time out, or user didn't login, 
      save the target url temporary.     
    11. JDBCUserFactory uf = new JDBCUserFactory();     
    12. if ( (this.request.getParameter("userID")==null)     
    13. || (this.request.getParameter("password")==null) )    
    14. {     
    15. throw new ACLException("Please input a valid 
      userName andpassword."); }     
    16. JDBCUser user = (JDBCUser) uf.UserLogin(   
    17. this.request.getParameter("userID"),   
    18. this.request.getParameter("password") );     
    19. user.touchLoginTime();     
    20. this.session.setAttribute("user",user);   
    21. this.session.setAttribute("BindingNotify",new 
      HttpSessionBinding(application));    
    22. }     
    23. }  

    When using Login, add the User and the BindingNotofy class to the session. When logout is used, the session is automatically deleted from the activeSessions vector.

     
     
    1. public void logout()    
    2. throws SQLException,ACLException    
    3. {     
    4. if (this.user == null && this.session.getAttribute("user")==null)     
    5. {    
    6. return;     
    7. }      
    8. Vector activeSessions = (Vector)this.application.
      getAttribute("activeSessions");     
    9. if (activeSessions != null)     
    10. {  
    11. activeSessions.remove(this.session);  
    12. application.setAttribute("activeSessions",activeSessions);     
    13. }      
    14. java.util.Enumeration e = this.session.getAttributeNames();      
    15. while (e.hasMoreElements())     
    16. {    
    17. String s = (String)e.nextElement();  
    18. this.session.removeAttribute(s);     
    19. }     
    20. this.user.touchLogoutTime();     
    21. this.user = null;    
    22. }  
     
    
    1. Aside from JSP, start with JSF.
    2. JSF and JSP are new partners
    3. Analysis of Application in JSP program
    4. Implement JSP page and code separation using JavaBean
    5. Integrate FCKEditor in JSF/JSP

    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.