/**
* 编写以下SessionCounter.java
* 并编译为SessiionCounter.class
* 然后放到你的网站的classpath的
* SessionCount(自己建立此目录)下面
*/
package SessionCount;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionCounter implements HttpSessionListener {
private static int activeSessions = 0;
public void sessionCreated(HttpSessionEvent se) {
activeSessions++;
}
public void sessionDestroyed(HttpSessionEvent se) {
if(activeSessions > 0)
activeSessions--;
}
public static int getActiveSessions() {
return activeSessions;
}
}
接着建立online.jsp文件用于显示在线人数
<%@ page import="SessionCount.SessionCounter" %>
在线:<%= SessionCounter.getActiveSessions() %>
然后需要在你的网站的WEB-INF中建立web.xml
文件内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
<web-app>
<!-- Listeners -->
<listener>
<listener-class>
SessionCount.SessionCounter
</listener-class>
</listener>
</web-app>
然后重新启动你的应用服务器,访问online.jsp检查是否显示正确。
该方法没有考虑到用户直接关闭浏览器的情况,这种情况下用户直到session失效才能移除
解决方法如下:(关闭浏览器使session立即失效的方法)
在页面上加<body onUnload="removeline()">
<%@ page language = "java"%>
<html>
<head>
<script language="javaScript">
function removeline()
{
<!--判断浏览器是关闭还是刷新 ,因为刷新也会调用onunload-->
if(event.clientX<0 && event.clientY<0)
{
document.write('<iframe width="100" height="100" src="removeSessionData.jsp"></iframe><OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>');
<!--是无提示的关闭浏览器-->
document.all.WebBrowser.ExecWB(45,1);
}
}
</script>
</head>
<body onUnload="removeline()">
</body>
</html>
你需要在没个页面中使用<jsp:include page="removeSession.jsp" flush="true" />来解决关闭浏览器清楚session的问题,但使用这种方法不能解决使用中断进程方法的session问题,这种情况下只有等到session超时才能清除session.
下面是关闭session的页面代码
<%@ page language="java"%>
<%
session.invalidate();
%>
----------------------------------------
相同的,监听attribute方法代码如下:
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionAttribListener implements HttpSessionAttributeListener {
//Creates new SessionAttribListen
public SessionAttribListen( )
{
}
public void attributeAdded(HttpSessionBindingEvent se)
{
HttpSession session = se.getSession();
String id = session.getId();
String name = se.getName();
String value = (String) se.getValue();
String source = se.getSource().getClass().getName();
//这里只是打印,你可以把它放在Session里的某个Map或者List里,
//这Map或者List就是所有用户的列表了
String message = new StringBuffer(
"Attribute bound to session in ").append(source).
append("/nThe attribute name: ").append(name).
append("/n").append("The attribute value:").
append(value).append("/n").
append("The session ID: ").
append(id).toString();
System.out.println(message);
}
public void attributeRemoved(HttpSessionBindingEvent se)
{
HttpSession session = se.getSession();
String id = session.getId();
String name = se.getName();
if (name == null)
{
name = "Unknown";
}
String value = (String) se.getValue();
String source = se.getSource().getClass().getName();
//这里只是打印,你可以把它放在Session里的某个Map或者List里,
//这Map或者List就是所有用户的列表了,此时别忘了从Map或者List移走这个id
String message = new StringBuffer(
"Attribute unbound from session in ").append(source).
append("/nThe attribute name: ").append(name).
append("/n").append("The attribute value: ").
append(value).append("/n").append(
"The session ID: ").append(id).toString();
System.out.println(message);
}
public void attributeReplaced(HttpSessionBindingEvent se)
{
String source = se.getSource().getClass().getName();
String message = new StringBuffer(
"Attribute replaced in session ").
append(source).toString();
System.out.println(message);
}
}
当然了,以上是跟踪所有的Session Attribute, 你自己可以根据你的需要跟踪知道的Attribute,
se.getName( );
可以取得Attribute name.
最后别忘了在Web.xml里申明,可以如下:
<listener>
<listener-class>packagename.SessionAttribListener</listener-class>
</listener>
------------------------实现在线用户统计
public static List list=new ArrayList(); 定义list存储用户登陆名称
public void attributeAdded(HttpSessionBindingEvent se)
{
String name = se.getName() == null ? "" : se.getName();
String value = (String) se.getValue() == null ? "" : (String) se.getValue();
if (name.equals("user") && !list.contains(value))
{
list.add(value);
}
}
public void attributeRemoved(HttpSessionBindingEvent se)
{
String name = se.getName() == null ? "" : se.getName();
String value = (String) se.getValue() == null ? "" : (String) se.getValue();
if (name.equals("user") && list.contains(value))
{
list.remove(value);
}
}
页面上只要取出SessionAttribListener.list,就可以得到用户列表和用户数量了
------------------------
同样的
public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce);
这两个接口可以实现应用的启动和停止事件,同样需要在web.xml中定义listener
<listener>
<listener-class>
com.stardeveloper.web.listener.ApplicationWatch</listener-class>
</listener>