统计在线人数

HttpSessionListener有2个接口需要实现

sessionCreated //新建一个会话时候触发也可以说是客户端第一次和服务器交互时候触发

sessionDestroyed //销毁会话的时候 一般来说只有某个按钮触发进行销毁 或者配置定时销毁 ( 很多文献中提到说浏览器关闭时候会销毁 但是楼主通过各种现行主流浏览器测试效果不尽如人意)

HttpSessionAttributeListener有3个接口需要实现

attributeAdded //在session中添加对象时触发此操作 笼统的说就是调用setAttribute这个方法时候会触发的

attributeRemoved //修改、删除session中添加对象时触发此操作 笼统的说就是调用 removeAttribute这个方法时候会触发的

attributeReplaced //在Session属性被重新设置时

以下是一个统计在线会话数的功能,并且让超时的自动销毁

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <listener>
  7. <listener-class>
  8. org.xiosu.listener.onlineListener
  9. </listener-class>
  10. </listener>
  11. <!--默认的会话超时时间间隔,以分钟为单位 -->
  12. <session-config>
  13. <session-timeout>1</session-timeout>
  14. </session-config>
  15. <welcome-file-list>
  16. <welcome-file>index.jsp</welcome-file>
  17. </welcome-file-list>
  18. </web-app>
<?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">
	<listener>
		<listener-class>
			org.xiosu.listener.onlineListener
		</listener-class>
	</listener>


	<!--默认的会话超时时间间隔,以分钟为单位  -->
	<session-config>
		<session-timeout>1</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

onlineListener.java

  1. package org.xiosu.listener;
  2. import java.util.ArrayList;
  3. import javax.servlet.ServletContext;
  4. import javax.servlet.http.HttpSessionAttributeListener;
  5. import javax.servlet.http.HttpSessionBindingEvent;
  6. import javax.servlet.http.HttpSessionEvent;
  7. import javax.servlet.http.HttpSessionListener;
  8. public class onlineListener implements HttpSessionListener,
  9. HttpSessionAttributeListener {
  10. // 参数
  11. ServletContext sc;
  12. ArrayList list = new ArrayList();
  13. // 新建一个session时触发此操作
  14. public void sessionCreated(HttpSessionEvent se) {
  15. sc = se.getSession().getServletContext();
  16. System.out.println("新建一个session");
  17. }
  18. // 销毁一个session时触发此操作
  19. public void sessionDestroyed(HttpSessionEvent se) {
  20. System.out.println("销毁一个session");
  21. if (!list.isEmpty()) {
  22. list.remove((String) se.getSession().getAttribute("userName"));
  23. sc.setAttribute("list", list);
  24. }
  25. }
  26. // 在session中添加对象时触发此操作,在list中添加一个对象
  27. public void attributeAdded(HttpSessionBindingEvent sbe) {
  28. list.add((String) sbe.getValue());
  29. System.out.println(sbe.getValue());
  30. sc.setAttribute("list", list);
  31. }
  32. // 修改、删除session中添加对象时触发此操作
  33. public void attributeRemoved(HttpSessionBindingEvent arg0) {
  34. System.out.println("5555555");
  35. }
  36. public void attributeReplaced(HttpSessionBindingEvent arg0) {
  37. System.out.println("77777777");
  38. }
  39. }


index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://"
  5. + request.getServerName() + ":" + request.getServerPort()
  6. + path + "/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>My JSP 'index.jsp' starting page</title>
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. <!--
  19. <link rel="stylesheet" type="text/css" href="styles.css">
  20. -->
  21. </head>
  22. <body>
  23. <%
  24. session = request.getSession(false);
  25. if (session != null)
  26. session.invalidate();
  27. %>
  28. <form action="isOnline.jsp" method="post">
  29. 用户名:
  30. <input type="text" name="uName" />
  31. <input type="submit" value="上线">
  32. </body>
  33. </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<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>
		<%
			session = request.getSession(false);

			if (session != null)
				session.invalidate();
		%>
		<form action="isOnline.jsp" method="post">
			用户名:
			<input type="text" name="uName" />
			<input type="submit" value="上线">
	</body>
</html>


isOnline.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'isOnline.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. session=request.getSession();
  23. session.setAttribute("userName",request.getParameter("uName"));
  24. response.sendRedirect("showOnline.jsp");
  25. %>
  26. </body>
  27. </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'isOnline.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>
    <%

session=request.getSession();

session.setAttribute("userName",request.getParameter("uName"));

response.sendRedirect("showOnline.jsp");
%>
  </body>
</html>


showOnline.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'showOnline.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. ArrayList showList=(ArrayList)(getServletContext().getAttribute("list"));
  23. out.print("在线人数 "+showList.size()+"<br>");
  24. for(int i=0;i<showList.size();i++){
  25. out.print(showList.get(i)+"在线"+"<br>");
  26. }
  27. %>
  28. <br>
  29. <a href="index.jsp">退出</a>
  30. </body>
  31. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值