[java]HttpSessionListener实现统计在线人数

        HttpSessionListener是个session监听器,它有两个方法:public void sessionCreated(HttpSessionEvent event){}   和  public void sessionDestroyed(HttpSessionEvent event){}  , 前者是在session被创建的时候执行,后者是在session被销毁的时候执行,通过对当前session的监听,达到统计在线人数的效果。

       代码如下:

       首先建一个监听类 CountLineListener,实现 HttpSessionListener 接口,并添加未实现的方法 sessionCreated(){}  和 sessionDestroyed(){}:

 

package com.test.listener;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class CountLineListener implements HttpSessionListener{

	/***********
	 * 创建session时调用
	 */
	public void sessionCreated(HttpSessionEvent event) {
		System.out.println("创建session......");
		ServletContext context=event.getSession().getServletContext();
		Integer count=(Integer)context.getAttribute("count");
		if(count==null){
			count=new Integer(1);
		}else{
			int co = count.intValue( );
			count= new Integer(co+1);
		}
		System.out.println("当前用户人数:"+count);
		context.setAttribute("count", count);//保存人数
		
	}

	/************
	 * 销毁session时调用
	 */
	public void sessionDestroyed(HttpSessionEvent event) {
		System.out.println("销毁session......");
		ServletContext context=event.getSession().getServletContext();
		Integer count=(Integer)context.getAttribute("count");
		int co=count.intValue();
		count=new Integer(co-1);
		context.setAttribute("count", count);
		System.out.println("当前用户人数:"+count);
	}

}

     监听类写好了,接下来就要在web.xml里配置此监听类,添加代码:
         <listener>
		<listener-class>com.test.listener.CountLineListener</listener-class>
	</listener>

   

      针对以上可以结合 servlet 写个小例子:

      (1) login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!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">
	
  </head>
  
  <body>
  <form method="POST" action="<%=request.getContextPath()%>/MyServlet">
  <input type="text" name="username"/>
  <br/><input type="submit" value="登录"/>
  </form>
  </body>
</html>


 

     点击登录---->MyServlet

     (2) MyServlet.java

package com.test.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String user= request.getParameter("username");
		request.getSession().setAttribute("user", user);
		request.getRequestDispatcher("/index.jsp").forward(request,response);
		
	}

}


     登录就跳到首页index.jsp,显示在线人数:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!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">
	
  </head>
  
  <body>
  这里是首页,当前访问量:
  <%
  ServletContext context=session.getServletContext();
  Integer count=(Integer)context.getAttribute("count");
  %>
  <%=count %>
  <br/>
  当前用户:${sessionScope.user }
   </body>
</html>


         这样就简单实现统计当前在线人数的效果了。如果在页面有一个"退出系统"的链接,可以调用session.invalidate()执行清除session,这样在线人数就会-1,那如果用户没有点击“退出系统”,而是直接关闭浏览器呢?我自己做了个测试,在关闭浏览器一会儿的话,服务器端会自动执行sessionDestroyed()方法进行销毁session,此时用户人数-1,但是有时候又不会执行,不知道为什么,自己还是慢慢研究吧......


最后,欢迎访问风格清新简洁的轻博客网站[指尖一刻]


 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值