Java基于servlet * 实现在线人数监控功能的方法
作者:小易Smalle 时间:2021-08-19 11:38:24
本文实例讲述了Java基于servlet * 实现在线人数监控功能的方法。分享给大家供大家参考,具体如下:
1、分析:
做一个网站在线人数统计,可以通过ServletContextListener监听,当Web应用上下文启动时,在ServletContext中添加一个List.用来准备存放在线的用户名,然后通过HttpSessionAttributeListener监听,当用户登录成功,把用户名设置到Session中。同时将用户名方法到ServletContext的List中,最后通过HttpSessionListener监听,当用户注销会话时,讲用户名从应用上下文范围中的List列表中删除。
2、注意事项
测试时,需要启动不同的浏览器来登陆不同的用户,只有点击注销按钮才能减少在线用户,关闭浏览器不能减少在线用户。
3、项目源代码
(1)java代码
OnlineListener类
package com.smalle.listener;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
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;
//应用上下文初始时会回调的方法
@Override
public void contextInitialized(ServletContextEvent e) {
//初始化一个application对象
application = e.getServletContext();
//设置一个列表属性,用于保存在线用户名
this.application.setAttribute("online", new LinkedList<String>());
}
//往会话中添加属性时的回调方法
@Override
public void attributeAdded(HttpSessionBindingEvent e) {
//取得用户名列表
List<String> onlines = (List<String>) this.application.getAttribute("online");
if("username".equals(e.getName())){
onlines.add((String) e.getValue());
}
//将添加后的列表重新设置列application属性中.
this.application.setAttribute("online", onlines);
}
//会话销毁时会回调的方法
@Override
public void sessionDestroyed(HttpSessionEvent e) {
//取得用户名列表
List<String> onlines = (List<String>) this.application.getAttribute("online");
//取得当前用户名
String username = (String) e.getSession().getAttribute("username");
//将此用户从列表中删除
onlines.remove(username);
//讲删除后的列表重新设置到application属性中.
this.application.setAttribute("online", onlines);
}
public void sessionCreated(HttpSessionEvent e) {}
public void attributeRemoved(HttpSessionBindingEvent e) {}
public void attributeReplaced(HttpSessionBindingEvent e) {}
public void contextDestroyed(ServletContextEvent e) {}
}
LoginServlet类
package com.smalle.listener;
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;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
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");
System.out.println("LoginServlet" + online);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("");
out.println(" <title>用户列表</title>");
out.println(" ");
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=\"" + response.encodeURL("logoutListener") + "\">注销</a>");
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
LogoutServlet类
package com.smalle.listener;
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;
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("");
out.println(" <title>用户列表</title>");
out.println(" ");
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='\'index.html\''>主页</a>");
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
(2)web.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>testServlet</display-name>
<listener>
<listener-class>com.smalle.listener.OnlineListener</listener-class>
</listener>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.smalle.listener.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginListener</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.smalle.listener.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logoutListener</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(3)表现层代码
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="loginListener" method="post">
用户名:<input type="text" name="username">
<input type="submit" value="登录"><br><br>
</form>
</body>
</html>
希望本文所述对大家java程序设计有所帮助。
来源:http://blog.csdn.net/oldinaction/article/details/40622393
标签:Java,servlet, ,
0
投稿
猜你喜欢
Java实现根据模板自动生成新的PPT
2022-10-22 18:31:31
逆波兰计算器(Java实现)
2021-10-18 09:45:01
详解springmvc常用5种注解
2023-03-27 15:15:43
Android开发实现的计时器功能示例
2023-09-04 07:33:33
SpringBoot如何注册Servlet、Filter、Listener的几种方式
2023-07-15 05:15:08
教大家使用java实现顶一下踩一下功能
2021-08-08 21:31:15
Java排序的那些事之sort方法的使用详解
2023-01-22 00:18:35
mybatis foreach遍历LIST读到数据为null的问题
2021-05-24 20:15:27
DUCC配置平台实现一个动态化线程池示例代码
2023-11-28 12:07:39
SpringCloud微服务架构实战之微服务治理功能的实现
2023-07-20 09:06:38
详解Java回环屏障CyclicBarrier
2022-08-30 15:02:09
SpringBoot整合Shiro框架,实现用户权限管理
2021-10-27 00:03:02
WinForm实现读取Resource中文件的方法
2021-05-25 18:29:45
java8学习教程之函数引用的使用方法
2023-08-28 12:03:19
C#下载歌词文件的同步和异步方法
2023-04-11 22:46:49
Struts2拦截器 关于解决登录的问题
2023-07-02 14:06:09
Java结构型设计模式之桥接模式详细讲解
2023-08-26 16:25:56
Android 源码如何编译调试
2023-09-03 07:46:40
Mybatis结果集自动映射的实例代码
2023-07-09 02:13:58
SpringBoot万字爆肝高级配置
2022-09-17 06:34:08