显示在线用户

方法一:

用application对象。 
简单描述如下: 
当用户登录时维护一个会话对象:UserInfo类的实例,同时UserInfo类实现javax.servlet.http.HttpSessionBindingListener接口: 
import java.util.Map; 
import java.util.HashMap; 
import javax.servlet.http.*; 
import javax.servlet.ServletContext; 

public class UserInfo implements HttpSessionBindingListener { 
// 用户信息 
int userId; 
String userName; 
...... 

/** 
* 当UserInfo类被绑定到session中时触发这个事件 
* 具体信息请参见Servlet/JSPs的规范 
*/ 
public void valueBound(HttpSessionBindingEvent event) { 
HttpSession session = event.getSession (); 
ServletContext ctx = session.getServletContext (); 
Map map = (Map)ctx.getAttribute ("users"); 
if (map == null) { // 如果是服务器启动后的第一个登录用户,新建一个容器 
map = new HashMap (); 
ctx.setAttribute ("users", map); 
} 

map.put (userName, this); // 把自己加入容器中 
} 

/** 
* 当UserInfo类从session中删除时触发这个事件 
* 具体信息请参见Servlet/JSPs的规范 
*/ 
public void valueUnbound(HttpSessionBindingEvent event) { 
HttpSession session = event.getSession (); 
ServletContext ctx = session.getServletContext (); 
Map map = (Map)ctx.getAttribute ("users"); 
map.remove (this); // 把自己从容器中删除 
} 

...... 
} 

以后在JSP中就可以这样显示在线用户数了: 
<% 
int userCount = 0; 
java.util.Map map = (java.util.Map)application.getAttribute ("users"); 
if (map == 0) userCount = 0; 
else userCount = map.size (); 
%>; 

当然你也可以不实现javax.servlet.http.HttpSessoinBindLisenter接口,而用自己的程序来实现这个功能,但这种方法最保险,也最简单。
如果用户关掉浏览器,过一段时间后session就会超时,这时发触发valueUnbound事件

方法二:
servlet:
package SessionCount;
import javax.servlet.*;
   import javax.servlet.http.*;
   public class SessionCounter implements HttpSessionListener {
   private static int activeSessions = 0;
   public void sessionCreated(HttpSessionEvent se) {
   activeSessions++;
   }
   public void sessionDestroyed(HttpSessionEvent se) {
   if(activeSessions > 0)
   activeSessions--;
   }
   public static int getActiveSessions() {
   return activeSessions;
   }
   }
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
  
   <listener>
   <listener-class>
   SessionCount.SessionCounter
   </listener-class>
   </listener>
   
   <session-config>
   <session-timeout>1</session-timeout>
</session-config>
</web-app>
test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="SessionCount.SessionCounter" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
    
     <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
   </head>
  
   <body>
     在线人数:<%=SessionCounter.getActiveSessions()%>
   </body>
</html>

 

你可能感兴趣的:(java,jsp,Web,servlet,css)