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

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

index.jsp

	
		


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");
   }
 %>


	
	  
	

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


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 map = (ConcurrentHashMap) 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 map = (ConcurrentHashMap) context.getAttribute("activeSessions");

        if (null == map)
        {
            map = new ConcurrentHashMap();
            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 map = (ConcurrentHashMap) context.getAttribute("activeSessions");

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

}



 

你可能感兴趣的:(JSP,Servlet)