使用监听器实现一个统计网站在线人数的示例
2016-12-01 20:29
701 查看
(1)创建一个监听器实现类
要大致统计一个网站的在线人数,首先,可以通过ServletContextListener监听,当Web应用上下文启动时,在ServletContext中添加一个List,用来准备存放在线的用户名;然后,可以通过HttpSessionAttributeListener监听,当用户登录成功把用户名设置到Session中时同时将用户名存放到ServletContext中的List列表中;最后通过HttpSessionListener监听,当用户注销会话时将用户名从应用上下文范围中的List列表中删除。
所以,编写OnLineListener类实现ServletContextListener、HttpSessionAttributeListener、HttpSessionListener接口,具体代码如下:
[java] view
plain copy
package com.web.servlet;
import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.util.LinkedList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
4000
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
//在线人数统计监听器实现类
public class OnlineListener implements ServletContextListener,
HttpSessionAttributeListener, HttpSessionListener {
private ServletContext application = null;
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent arg0) {
//初始化一个application对象
this.application = arg0.getServletContext();
//设置一个列表属性,用于保存在想用户名
this.application.setAttribute("online", new LinkedList<String>());
}
//往会话中添加属性时会回调的方法
public void attributeAdded(HttpSessionBindingEvent arg0) {
//取得用户名列表
List<String> online = (List<String>) this.application
.getAttribute("online");
if ("username".equals(arg0.getName())) {
//将当前用户名添加到列表中
online.add((String) arg0.getValue());
}
//将添加后的列表重新设置到application属性中
this.application.setAttribute("online", online);
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}
//会话销毁时会回调的方法
public void sessionDestroyed(HttpSessionEvent arg0) {
//取得用户名列表
List<String> online = (List<String>) this.application
.getAttribute("online");
//取得当前用户名
String username = (String) arg0.getSession().getAttribute("username");
//将此用户名从列表中删除
online.remove(username);
//将删除后的列表重新设置到application属性中
this.application.setAttribute("online", online);
}
}
(2)在web.xml中注册监听器
监听器实现好后,还需要在web.xml文件中进行注册才能起作用,只需要在web.xml中像如下添加元素即可
<!-- 注册一个监听器 -->
<listener>
<!-- 指定监听器实现类的全限定名 -->
<listener-class>
com.web.servlet.OnlineListener
</listener-class>
</listener
最后,我们创建几个Servlet来测试这个监听器实现的功能。
处理用户登录的Servlet类代码:
[c-sharp] view
plain copy
package com.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//处理用户登录的Servlet
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");//设置相应内容类型
String username= request.getParameter("username");//获取请求参数中的用户名
//往session中添加属性,会触发HttpSessionAttributeListener中的attributeAdded方法
if(username != null && !username.equals("")) {
request.getSession().setAttribute("username",username);
}
//从应用上下文中获取在线用户名列表
List<String> online = (List<String>)getServletContext().getAttribute("online");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>用户列表</TITLE></HEAD>");
out.println(" <BODY>");
out.println("当前用户是:" + username);
out.print(" <hr/><h3>在线用户列表</h3>");
int size = online == null ? 0 : online.size();
for (int i = 0; i < size; i++) {
if(i > 0){
out.println("<br/>");
}
out.println(i + 1 + "." + online.get(i));
}
//注意: 要对链接URL进行自动重写处理
out.println("<hr/><a href="/" mce_href="/""" + response.encodeURL("logout") + "/">注销</a>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
处理用户登录Servlet的类代码
[c-sharp] view
plain copy
package com.web.servlet;
import java.io.*;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
//处理用户注销会话的Servlet
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//销毁会话,会触发SessionLinstener中的sessionDestroyed方法
request.getSession().invalidate();
//从应用上下文中获取在线用户名列表
List<String> online = (List<String>)getServletContext().getAttribute("online");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>用户列表</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" <h3>在线用户列表</h3>");
int size = online == null ? 0 : online.size();
for (int i = 0; i < size; i++) {
if(i > 0){
out.println("<br/>");
}
out.println(i + 1 + "." + online.get(i));
}
out.println("<hr/><a href="/" mce_href="/""index.html/">主页</a>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
然后创建一个index.html文件,用来供用户登录,代码如下:
[xhtml] view
plain copy
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>index.html</title>
</head>
<body>
<form action = "login" method = "post">
用户名:<input type ="text" name = "username"/>
<input type = "submit" value = "登录"/><br/><br/>
</form>
</body>
</html>
把WEB部署到Tomcat容器总,并启动。打开浏览器访问即可
要大致统计一个网站的在线人数,首先,可以通过ServletContextListener监听,当Web应用上下文启动时,在ServletContext中添加一个List,用来准备存放在线的用户名;然后,可以通过HttpSessionAttributeListener监听,当用户登录成功把用户名设置到Session中时同时将用户名存放到ServletContext中的List列表中;最后通过HttpSessionListener监听,当用户注销会话时将用户名从应用上下文范围中的List列表中删除。
所以,编写OnLineListener类实现ServletContextListener、HttpSessionAttributeListener、HttpSessionListener接口,具体代码如下:
[java] view
plain copy
package com.web.servlet;
import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.util.LinkedList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
4000
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
//在线人数统计监听器实现类
public class OnlineListener implements ServletContextListener,
HttpSessionAttributeListener, HttpSessionListener {
private ServletContext application = null;
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent arg0) {
//初始化一个application对象
this.application = arg0.getServletContext();
//设置一个列表属性,用于保存在想用户名
this.application.setAttribute("online", new LinkedList<String>());
}
//往会话中添加属性时会回调的方法
public void attributeAdded(HttpSessionBindingEvent arg0) {
//取得用户名列表
List<String> online = (List<String>) this.application
.getAttribute("online");
if ("username".equals(arg0.getName())) {
//将当前用户名添加到列表中
online.add((String) arg0.getValue());
}
//将添加后的列表重新设置到application属性中
this.application.setAttribute("online", online);
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}
//会话销毁时会回调的方法
public void sessionDestroyed(HttpSessionEvent arg0) {
//取得用户名列表
List<String> online = (List<String>) this.application
.getAttribute("online");
//取得当前用户名
String username = (String) arg0.getSession().getAttribute("username");
//将此用户名从列表中删除
online.remove(username);
//将删除后的列表重新设置到application属性中
this.application.setAttribute("online", online);
}
}
(2)在web.xml中注册监听器
监听器实现好后,还需要在web.xml文件中进行注册才能起作用,只需要在web.xml中像如下添加元素即可
<!-- 注册一个监听器 -->
<listener>
<!-- 指定监听器实现类的全限定名 -->
<listener-class>
com.web.servlet.OnlineListener
</listener-class>
</listener
最后,我们创建几个Servlet来测试这个监听器实现的功能。
处理用户登录的Servlet类代码:
[c-sharp] view
plain copy
package com.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//处理用户登录的Servlet
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");//设置相应内容类型
String username= request.getParameter("username");//获取请求参数中的用户名
//往session中添加属性,会触发HttpSessionAttributeListener中的attributeAdded方法
if(username != null && !username.equals("")) {
request.getSession().setAttribute("username",username);
}
//从应用上下文中获取在线用户名列表
List<String> online = (List<String>)getServletContext().getAttribute("online");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>用户列表</TITLE></HEAD>");
out.println(" <BODY>");
out.println("当前用户是:" + username);
out.print(" <hr/><h3>在线用户列表</h3>");
int size = online == null ? 0 : online.size();
for (int i = 0; i < size; i++) {
if(i > 0){
out.println("<br/>");
}
out.println(i + 1 + "." + online.get(i));
}
//注意: 要对链接URL进行自动重写处理
out.println("<hr/><a href="/" mce_href="/""" + response.encodeURL("logout") + "/">注销</a>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
处理用户登录Servlet的类代码
[c-sharp] view
plain copy
package com.web.servlet;
import java.io.*;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
//处理用户注销会话的Servlet
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//销毁会话,会触发SessionLinstener中的sessionDestroyed方法
request.getSession().invalidate();
//从应用上下文中获取在线用户名列表
List<String> online = (List<String>)getServletContext().getAttribute("online");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>用户列表</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" <h3>在线用户列表</h3>");
int size = online == null ? 0 : online.size();
for (int i = 0; i < size; i++) {
if(i > 0){
out.println("<br/>");
}
out.println(i + 1 + "." + online.get(i));
}
out.println("<hr/><a href="/" mce_href="/""index.html/">主页</a>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
然后创建一个index.html文件,用来供用户登录,代码如下:
[xhtml] view
plain copy
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>index.html</title>
</head>
<body>
<form action = "login" method = "post">
用户名:<input type ="text" name = "username"/>
<input type = "submit" value = "登录"/><br/><br/>
</form>
</body>
</html>
把WEB部署到Tomcat容器总,并启动。打开浏览器访问即可
相关文章推荐
- java使用监听器实现一个统计网站在线人数的示例
- 使用监听器实现一个统计网站在线人数的示例
- 如何使用Servlet监听器实现网站在线人数统计
- 利用监听器实现网站在线人数统计
- web.xml中listener作用及使用--实现记录网站的客户登录日志, 统计在线人数
- Servlet监听器实现网站在线人数统计
- 使用ServletContextListener和HttpSessionListener两种监听器实现记录当前网站在线人数
- Servlet监听器的简单使用实现统计在线人数
- Java监听器实现网站在线人数统计
- 【附】使用监听器ServletContextListener实现网站登录人数统计,可以实现重启服务器后记录仍然存在
- 对jsp的一个小结(8)使用servlet控制新闻管理、使用过滤器(解决乱码)和监听器(统计在线用户人数)
- 使用java 监听器实现在线人数统计
- java session HttpSessionListener、HttpSessionBindingListener使用区别,实现在线人数统计以及踢出用户
- 使用servlet统计网站在线人数的方法
- 利用HttpSessionListener实现网站在线人数统计功能
- 一个统计当前在线用户的解决方案,可以在聊天室、论坛、网站中使用
- 专题:如何实现 网站在线人数以及历史访问人数的统计
- 监听器(实现在线统计人数,踢人)
- Servlet监听器简介与实现在线人数统计
- java监听器示例-统计在线人数