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