这个接口主要是用来监控session的创建和消毁的,它有两个方法
我们要实现监控session的功能,需要实现这个接口,然后在web .xml中配置一个Listener.
- public void sessionCreated(HttpSessionEvent se)
- public void sessionDestroyed(HttpSessionEvent se)
- /**
- * 编写以下SessionCounter.java
- * 并编译为SessiionCounter.class
- * 然后放到你的网站的classpath的
- * SessionCount(自己建立此目录)下面
- */
- 1.
- 2. package SessionCount;
- 3. import javax.servlet.*;
- 4. import javax.servlet.http.*;
- 5.
- 6. public class SessionCounter implements HttpSessionListener {
- 7.
- 8. private static int activeSessions = 0;
- 9.
- 10. public void sessionCreated(HttpSessionEvent se) {
- 11. activeSessions++;
- 12. }
- 13.
- 14. public void sessionDestroyed(HttpSessionEvent se) {
- 15. if(activeSessions > 0)
- 16. activeSessions--;
- 17. }
- 18.
- 19. public static int getActiveSessions() {
- 20. return activeSessions;
- 21. }
- 22. }
- <%@ page import="SessionCount.SessionCounter" %>
- 在线:<%= SessionCounter.getActiveSessions() %>
- xml version="1.0" encoding="ISO-8859-1"?>
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
- <web-app>
- <listener>
- <listener-class>
- SessionCount.SessionCounter
- listener-class>
- listener>
- web-app>