Servlet器统计网站在线人数
本案例可以通过如下 2 种方案实现:
使用 HttpSessionListener 和 HttpSessionAttributeListener 实现;
使用 HttpSessionBindingListener 实现。
使用 HttpSessionListener 和 HttpSessionAttributeListener 实现
1. 创建一个名称为 listennerDemo 的工程,并分别创建 net.biancheng.www.listener 和 net.biancheng.www.servlet 两个包。2. 在 WebContent 中,创建 login.html,代码如下。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3网站站点" rel="nofollow" />
package net.biancheng.www.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 登录校验
* @author 编程帮 www.biancheng网站站点" rel="nofollow" />
package net.biancheng.www.listener;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* 器
* @author 编程帮 www.bianchengbang网站站点" rel="nofollow" />
package net.biancheng.www.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 退出登录
* @author 编程帮 www.bianchengbang网站站点" rel="nofollow" />
7. 输入账号信息后,点击提交,结果如下图。

8. 打开火狐浏览器,访问 login.html,填写账号信息,结果如下图。

9. 点击提交,结果如下图。

10. 在谷歌浏览器点击退出登录,回到登录首页,切换到火狐浏览器刷新页面,结果如图。

使用 HttpSessionBindingListener 实现
1. 创建一个名称为 sessionBindingDemo 的工程,并分别创建 net.biancheng.www.listener 和 net.biancheng.www.servlet 两个包。2 .在 WebContent 中,创建 login.html,代码如下。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3网站站点" rel="nofollow" />
package net.biancheng.www.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.biancheng.www.listener.MyBindingListener;
/**
* 登录
*
* @author 编程帮 www.bianchengbang网站站点" rel="nofollow" />
package net.biancheng.www.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 退出登录
* @author 编程帮 www.bianchengbang网站站点" rel="nofollow" />
package net.biancheng.www.listener;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/**
* 使用MyBindingListener
*
* @author 编程帮 www.bianchengbang网站站点" rel="nofollow" />