您的位置:首页 > 理论基础 > 计算机网络

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

2015-08-13 12:07 981 查看
[java]HttpSessionListener实现统计在线人数

2012-12-11 0 个评论
作者:shenfuding_cn
收藏

我要投稿


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

代码如下:

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



[java]

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里配置此监听类,添加代码:

[java]

<listener>

<listener-class>com.test.listener.CountLineListener</listener-class>

</listener>



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

(1) login.jsp

[html]

<%@ 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

[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,显示在线人数:

[html]

<%@ 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,但是有时候又不会执行,不知道为什么,自己还是慢慢研究吧......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: