session监听器 统计在线人数 2

第4步 显示在线用户的情况。

可以直接通过 SessionListener.getSessions()方法来取得所有在线的用户。
Html代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=%3C%25%40%20page%20contentType%3D%22text%2Fhtml%3Bcharset%3DUTF-8%22%25%3E%0A%3C%25%40%20page%20import%3D%22java.util.List%22%25%3E%0A%3C%25%40%20page%20import%3D%22java.util.Iterator%22%25%3E%0A%3C%25%40%20page%20import%3D%22com.stephen.filter.SessionListener%22%25%3E%0A%3C%25%40%20page%20import%3D%22com.stephen.filter.OnlineSession%22%25%3E%0A%3C!DOCTYPE%20HTML%20PUBLIC%20%22-%2F%2FW3C%2F%2FDTD%20HTML%204.01%20Transitional%2F%2FEN%22%3E%0A%3Chtml%3E%3Chead%3E%3Ctitle%3EOnline%20session%20Query%3C%2Ftitle%3E%0A%3Cmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%3E%0A%3C%2Fhead%3E%3Cbody%20bgcolor%3D%22%23e5ecf9%22%20topmargin%3D%225px%22%20leftmargin%3D%225px%22%20rightmargin%3D%225px%22%3E%0A%3C%25%0A%09List%20sessions%20%3D%20SessionListener.getSessions()%3B%0A%09String%20pageErrorInfo%20%3D%20null%3B%0A%09try%7B%0A%25%3E%0A%09%09%3Ch5%3EOnline%20Employee%3C%2Fh5%3E%0A%09%09%0A%09%09%3Ctable%20width%3D%22100%25%22%20align%3D%22center%22%20cellspacing%3D%221%22%20cellpadding%3D%224%22%20border%3D%220%22%3E%0A%09%09%3Ctr%20id%3D%22listTitle%22%3E%0A%09%09%3Ctd%3ENo.%3C%2Ftd%3E%0A%09%09%3Ctd%3E%E7%99%BB%E5%BD%95Id%3C%2Ftd%3E%0A%09%09%3Ctd%3E%E7%99%BB%E5%BD%95%E6%97%B6%E9%97%B4%3C%2Ftd%3E%0A%09%09%3Ctd%3Eip%3C%2Ftd%3E%0A%09%09%3C%2Ftr%3E%0A%3C%25%0A%09%09StringBuffer%20datas%20%3D%20new%20StringBuffer()%3B%0A%09%09if(sessions!%3Dnull)%7B%0A%09%09%09OnlineSession%20onlineSession%20%3D%20null%3B%0A%09%09%09Iterator%20it%20%3D%20sessions.iterator()%3B%0A%09%09%09int%20count%3D0%3B%0A%09%09%09while(it.hasNext())%7B%0A%09%09%09%09count%2B%2B%3B%0A%09%09%09%09onlineSession%3D(OnlineSession)it.next()%3B%0A%09%09%09%09datas.append(%22%3Ctr%3E%3Ctd%3E%22)%3B%0A%09%09%09%09datas.append(count)%3B%0A%09%09%09%09datas.append(%22%3C%2Ftd%3E%3Ctd%3E%22)%3B%0A%09%09%09%09datas.append(onlineSession.getLoginId())%3B%0A%09%09%09%09datas.append(%22%3C%2Ftd%3E%3Ctd%3E%22)%3B%0A%09%09%09%09datas.append(onlineSession.getOnlineTime())%3B%0A%09%09%09%09datas.append(%22%3C%2Ftd%3E%3Ctd%3E%22)%3B%0A%09%09%09%09datas.append(onlineSession.getIp())%3B%0A%09%09%09%09datas.append(%22%3C%2Ftd%3E%3C%2Ftr%3E%22)%3B%0A%09%09%09%7D%0A%09%20%20%20%20%7D%0A%09%20%20%20%20out.print(datas.toString())%3B%0A%25%3E%0A%09%09%3C%2Ftable%3E%0A%3C%25%0A%09%7Dcatch(Exception%20e)%7B%0A%09%09pageErrorInfo%20%3D%20e.toString()%3B%0A%09%7Dfinally%7B%0A%09%09if(pageErrorInfo!%3Dnull)%7B%0A%25%3E%0A%09%09%09%3Cfont%20color%3D%22red%22%3EError%3A%3C%25%3DpageErrorInfo%25%3E%3C%2Ffont%3E%0A%3C%25%0A%09%09%7D%0A%25%3E%0A%09%09%3C%2Fbody%3E%3C%2Fhtml%3E%0A%3C%25%09%09%0A%09%7D%0A%25%3E%0A" src="http://stephen830.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1235637043413" lk_media="yes">
  1. <%@ page contentType="text/html;charset=UTF-8"%>  
  2. <%@ page import="java.util.List"%>  
  3. <%@ page import="java.util.Iterator"%>  
  4. <%@ page import="com.stephen.filter.SessionListener"%>  
  5. <%@ page import="com.stephen.filter.OnlineSession"%>  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html><head><title>Online session Query</title>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. </head><body bgcolor="#e5ecf9" topmargin="5px" leftmargin="5px" rightmargin="5px">  
  10. <%  
  11.      List sessions = SessionListener.getSessions();  
  12.      String pageErrorInfo = null;  
  13.      try{  
  14. %>  
  15.         <h5>Online Employee</h5>  
  16.           
  17.         <table width="100%" align="center" cellspacing="1" cellpadding="4" border="0">  
  18.         <tr id="listTitle">  
  19.         <td>No.</td>  
  20.         <td>登录Id</td>  
  21.         <td>登录时间</td>  
  22.         <td>ip</td>  
  23.         </tr>  
  24. <%  
  25.          StringBuffer datas = new StringBuffer();  
  26.          if(sessions!=null){  
  27.              OnlineSession onlineSession = null;  
  28.              Iterator it = sessions.iterator();  
  29.              int count=0;  
  30.              while(it.hasNext()){  
  31.                  count++;  
  32.                 onlineSession=(OnlineSession)it.next();  
  33.                  datas.append("<tr><td>");  
  34.                  datas.append(count);  
  35.                  datas.append("</td><td>");  
  36.                  datas.append(onlineSession.getLoginId());  
  37.                  datas.append("</td><td>");  
  38.                  datas.append(onlineSession.getOnlineTime());  
  39.                  datas.append("</td><td>");  
  40.                  datas.append(onlineSession.getIp());  
  41.                  datas.append("</td></tr>");  
  42.              }  
  43.          }  
  44.          out.print(datas.toString());  
  45. %>  
  46.         </table>  
  47. <%  
  48.      }catch(Exception e){  
  49.         pageErrorInfo = e.toString();  
  50.      }finally{  
  51.          if(pageErrorInfo!=null){  
  52. %>  
  53.             <font color="red">Error:<%=pageErrorInfo%></font>  
  54. <%  
  55.          }  
  56. %>  
  57.         </body></html>  
  58. <%         
  59.      }  
  60. %>  
<%@ page contentType="text/html;charset=UTF-8"%> <%@ page import="java.util.List"%> <%@ page import="java.util.Iterator"%> <%@ page import="com.stephen.filter.SessionListener"%> <%@ page import="com.stephen.filter.OnlineSession"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>Online session Query</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head><body bgcolor="#e5ecf9" topmargin="5px" leftmargin="5px" rightmargin="5px"> <% List sessions = SessionListener.getSessions(); String pageErrorInfo = null; try{ %> <h5>Online Employee</h5> <table width="100%" align="center" cellspacing="1" cellpadding="4" border="0"> <tr id="listTitle"> <td>No.</td> <td>登录Id</td> <td>登录时间</td> <td>ip</td> </tr> <% StringBuffer datas = new StringBuffer(); if(sessions!=null){ OnlineSession onlineSession = null; Iterator it = sessions.iterator(); int count=0; while(it.hasNext()){ count++; onlineSession=(OnlineSession)it.next(); datas.append("<tr><td>"); datas.append(count); datas.append("</td><td>"); datas.append(onlineSession.getLoginId()); datas.append("</td><td>"); datas.append(onlineSession.getOnlineTime()); datas.append("</td><td>"); datas.append(onlineSession.getIp()); datas.append("</td></tr>"); } } out.print(datas.toString()); %> </table> <% }catch(Exception e){ pageErrorInfo = e.toString(); }finally{ if(pageErrorInfo!=null){ %> <font color="red">Error:<%=pageErrorInfo%></font> <% } %> </body></html> <% } %>


最后的说明:

如果你的系统中有注销(或者退出登录)类似的功能(功能中调用了 session.invalidate() 方法),那么用户的信息就会自动从上面的列表中删除。如果没有使用注销功能,则只能等到session超时候才能从列表中自动删除。

你可能感兴趣的:(session)