1. 这个文件用来记录用户 用户在线个数 以及用户名密码  
  2. import java.util.HashMap;  
  3. import java.util.Iterator;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6.  
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9.  
  10. /**  
  11. * @author heli  
  12. *   
  13. */  
  14. public class OnlineUser {  
  15.  
  16. private static long online = 0;  
  17.  
  18. private static Map userNameMap = new HashMap();// 目前只提供在线用户中文名字  
  19.              // 以后如果有需求可以考虑扩充  
  20.  
  21. private static Log log = LogFactory.getLog(OnlineUser.class);  
  22.  
  23. public static long getOnline() {  
  24.  
  25. return online;  
  26.  
  27. }  
  28.  
  29. public static boolean userOnline(String memEName, String memCName) {  
  30. log.debug("创建用户Session");  
  31. try {  
  32.    log.debug("用户名为:" + memEName);  
  33.    log.debug("userNameMap" + userNameMap);  
  34.    if (userNameMap.containsKey(memEName)) {  
  35.     log.debug("用户重复登陆");  
  36.     return false;  
  37.    } else {  
  38.     userNameMap.put(memEName, memCName);  
  39.     return true;  
  40.    }  
  41. } catch (Exception e) {  
  42.    log.error("将用户信息放入userNameMap失败,信息如下:" + e.getMessage());  
  43. }  
  44. log.debug("userNameMap" + userNameMap);  
  45.  
  46. return false;  
  47. }  
  48.  
  49. public static void raise() {  
  50.  
  51. online++;  
  52. log.debug("当前在线用户个数" + online);  
  53. }  
  54.  
  55. public static void reduce() {  
  56.  
  57. online--;  
  58. log.debug("当前在线用户个数" + online);  
  59.  
  60. }  
  61.  
  62. public static void userOffline(String memEName) {  
  63. // TODO 自动生成方法存根  
  64. log.debug("清除用户信息");  
  65. try {  
  66.    log.debug("用户注销 用户名为:" + memEName);  
  67.    userNameMap.remove(memEName);  
  68. } catch (Exception e) {  
  69.    log.error("清除用户信息失败:" + e.getMessage());  
  70. }  
  71. }  
  72.  
  73. public static void main(String arge[]) {  
  74. System.out.println("+++++++++++++++==");  
  75. Map map = new HashMap();  
  76. map.put("1""a");  
  77. map.put("2""b");  
  78. map.put("3""c");  
  79. map.put("4""d");  
  80. map.put("5""e");  
  81. Set set = map.keySet();  
  82. Iterator ito = set.iterator();  
  83. while (ito.hasNext())  
  84.    System.out.println(ito.next());  
  85. }  
  86.  
  87. /**  
  88. * @return userNameMap  
  89. */  
  90. public static Map getUserNameMap() {  
  91. return userNameMap;  
  92. }  
  93.  
  94. /**  
  95. * @param userNameMap  
  96. *            要设置的 userNameMap  
  97. */  
  98. public static void setUserNameMap(Map userNameMap) {  
  99. OnlineUser.userNameMap = userNameMap;  
  100. }  
  101.  
  102. }  
  103.  
  104. 这个用户用来实现对用户登陆,注销,以及session 过期用户的处理  
  105.  
  106.  
  107. package com.yc.ycportal.userutil;  
  108.  
  109. /**  
  110. * @author heli  
  111. *   
  112. */  
  113. import javax.servlet.http.HttpSessionEvent;  
  114.  
  115. import javax.servlet.http.HttpSessionListener;  
  116.  
  117. import org.apache.commons.logging.Log;  
  118. import org.apache.commons.logging.LogFactory;  
  119.  
  120. import com.yc.ycportal.user.User;  
  121. import com.yc.ycportal.util.Constants;  
  122.  
  123. public class OnlineUserListener implements HttpSessionListener {  
  124.  
  125. private static Log log = LogFactory.getLog(OnlineUserListener.class);  
  126.  
  127. public void sessionCreated(HttpSessionEvent hse) {  
  128. OnlineUser.raise();  
  129. }  
  130.  
  131. public void sessionDestroyed(HttpSessionEvent hse) {  
  132. User user = (User) hse.getSession().getAttribute(Constants.LONGIN_USER);  
  133. if (user == null)  
  134.    log.debug("捕获用户session注销操作,该用户为未登陆用户!");  
  135. else {  
  136.    try {  
  137.     OnlineUser.userOffline(user.getMember().getMemname());  
  138.    } catch (Exception e) {  
  139.     log.error("清除在线用户信息时,捕获用户名出错,错误信息如下:" + e.getMessage());  
  140.    }  
  141. }  
  142. OnlineUser.reduce();  
  143.  
  144. }  
  145.  
  146. }  
  147.  
  148.  
  149. 为了完成整个功能还需要 在用户登陆的时候调用userOnline(String memEName, String memCName)  
  150.  
  151. 用户注销的时候调用   public static void userOffline(String memEName) 以及session.invalidate()方法  
  152. 为了启动session监听需要在 web.xml中加入  
  153.  
  154.  
  155.       <listener id="listener_11111111111111">   
  156.         <listener-class>com.yc.ycportal.userutil.OnlineUserListener</listener-class>  
  157.       </listener>   
  158.     
  159.  
  160. 查看当前系统访问用户个数jsp如下:  
  161.  
  162. <%@ page contentType="text/html;charset=gb2312" %>  
  163.  
  164. <%@ page import="java.util.*"%>  
  165. <html>  
  166. <head>  
  167. <title>在线用户信息</title>  
  168. <meta http-equiv="refresh" content="20">  
  169. </head>  
  170.  
  171. <body>  
  172. <%  
  173.  
  174. Map map=com.yc.ycportal.userutil.OnlineUser.getUserNameMap();  
  175. out.println("目前已经登陆的用户如下:");  
  176.    out.println("<BR>");  
  177. Set set=map.keySet();  
  178. Iterator ito = set.iterator();  
  179. while(ito.hasNext()){  
  180.    String memName=""+ito.next();  
  181.    out.println("用户ID:"+memName);  
  182.    out.println("           用户名:"+map.get(memName));  
  183.    out.println("<BR>");  
  184.  
  185. }  
  186.  
  187. out.print("当前访问系统的总人数为:");  
  188. out.print(com.yc.ycportal.userutil.OnlineUser.getOnline());  
  189.  
  190. %>  
  191. </body>  
  192. </html>