JSP基于session,application的在线人数统计

JSP基于session,application的在线人数统计

index.jsp

	<body>
		<form action="LoginServlet" method="post">
			<input type="text" name="username" />
			<input type="submit" value="登陆" />
		</form>
	</body>


test.js

   window.onbeforeunload = function() //author: meizz    
    {    
       var n = window.event.screenX - window.screenLeft;    
       var b = n > document.documentElement.scrollWidth-20;    
       if(b && window.event.clientY < 0 || window.event.altKey)    
       {    
          window.location="LoginOutServlet";
                    // window.event.returnValue = "关闭提示"; //这里可以放置你想做的操作代码    
       }    
     }  

LoginServlet.java

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException,
            IOException
    {
        HttpSession session = request.getSession();
        LoginOutUtil.loginOut(this.getServletContext(), session);
        session.setMaxInactiveInterval(10);
        session.setAttribute("user", request.getParameter("username"));
        
        session.setAttribute("bindingNotify",
                             new ActiveSessionsListener(this.getServletContext()));
        response.sendRedirect("activeSessions.jsp");
    }


activeSession.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@page import="java.util.concurrent.ConcurrentHashMap"%>
<%@page import="java.util.Map.Entry"%>

<%
   if(session.getAttribute("user") == null)
   {
      response.sendRedirect("loginOut.jsp");
   }
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	  <script type="text/javascript" src="test.js"></script>
	</head>

	<body>
	过期时间为10秒钟, 10秒钟内不做任何操作就会过期.
   <br/>	
	<%ConcurrentHashMap<String, Object> sessionsMap = (ConcurrentHashMap<String, Object>)application.getAttribute("activeSessions"); %>
	当前在线的有:<%=sessionsMap.size() %>
	
	<hr/>
	 <% 
	  for (Map.Entry<String, Object>  entry: sessionsMap.entrySet())
        {
     %>
     <%=entry.getValue() %><br/>
     <%
        }
	 %>
	</body>
</html>


LoginOutServlet.java

package com.lzw;

import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

public class LoginOutUtil
{
    public static void loginOut(ServletContext context, HttpSession session)
    {
        if (session.getAttribute("user") == null)
        {
            return;
        }

        ConcurrentHashMap<String, Object> map = (ConcurrentHashMap<String, Object>) context.getAttribute("activeSessions");

        if (null != map)
        {
            session.invalidate();
            if (map.containsKey(session.getId()))
            {

                map.remove(session.getId());
                context.setAttribute("activeSessions", map);
            }
        }
    }
}


//上面的为LoginOutUtil.java
LoginOutServlet.java

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException,
            IOException
    { 
        if(request.getSession().getAttribute("user") != null)
        {
            LoginOutUtil.loginOut(this.getServletContext(), request.getSession());
            System.out.println(request.getSession().getAttribute("user") + "离开了...");
        }
    }


 

package com.lzw;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import java.util.Date;
import java.util.Set;
import java.util.Vector;
import java.util.Map.Entry;
import java.util.concurrent.*;

public class ActiveSessionsListener implements HttpSessionBindingListener
{
    private ServletContext context = null;


    public ActiveSessionsListener(ServletContext context)
    {
        if (null == context)
        {
            throw new IllegalArgumentException("Null servletContent is accept.");
        }
        this.context = context;
    }


    public void valueBound(HttpSessionBindingEvent sessionBindingEvent)
    {
        ConcurrentHashMap<String, Object> map = (ConcurrentHashMap<String, Object>) context.getAttribute("activeSessions");

        if (null == map)
        {
            map = new ConcurrentHashMap<String, Object>();
            context.setAttribute("activeSessions", map);
        }

        HttpSession session = sessionBindingEvent.getSession();
        map.put(session.getId(), session.getAttribute("user") + "   登陆时间:"
                                 + new Date().toLocaleString());

        context.setAttribute("activeSessions", map);
    }


    public void valueUnbound(HttpSessionBindingEvent sessionBindingEvent)
    {
        Object obj = null;
        try
        {

            obj = sessionBindingEvent.getSession().getAttribute("user");
        }
        catch (Exception e)
        {
            obj = null;
        }
        if (null == obj)
        {
            ConcurrentHashMap<String, Object> map = (ConcurrentHashMap<String, Object>) context.getAttribute("activeSessions");

            if (null != map)
            {
                if(map.containsKey(sessionBindingEvent.getSession().getId()))
                {
                    map.remove(sessionBindingEvent.getSession().getId());
                    context.setAttribute("activeSessions", map);
                }
            }
        }
    }

}



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值