JavaWeb-Listener2-在线用户统计
来源:互联网 发布:淘宝乔丹是真的吗 编辑:程序博客网 时间:2024/04/29 18:19
话说:
各位读者,早安!前面专门有一篇博客介绍Listener的,这篇来点好玩的。
我们要实现的功能是这样的:一尊贵的来宾来访问enter.jsp,登陆后,跳转到online.jsp,页面显示来宾姓名和在线人数;因为我们用的是HttpSessionBindingListener,所以我们用不同浏览器访问测试:
目的很简单,也是运用一下监听器喽。
这里用的是360先测试,一会用专业浏览器谷歌哈。
以下是这个玩耍过程的整体布局:
先上三个页面 :
enter.jsp
<%-- User: Meice Date: 2017/10/9 Time: 23:23--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body> <form action="login.do" method="post"> 来宾名:<input type="text" name="gustName"/><br/> <input type="submit" value="进入"> </form></body></html>
online.jsp
<%@ page import="com.hmc.listener.util.OnLineUtil" %><%-- User: Meice Date: 2017/10/10 Time: 8:07--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>统计在线人数</title> <% String username = request.getParameter("username"); %></head><body> <h2>您好: ${user.name}</h2> <h2>此时的在线人数为:<%=OnLineUtil.ONLINE_COUNT %> <br/></h2> <a href="loginOut.do">离开</a></body></html>
User
这个实体类,用来实现HttpSessionListener接口,实现登陆用户在线统计,是这里面最核心的功能
package com.hmc.listener.model;import com.hmc.listener.util.OnLineUtil;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;/** * User:Meice * 2017/10/11 */public class User implements HttpSessionBindingListener { private String name; public User() {} public User(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) { OnLineUtil.ONLINE_COUNT++; } @Override public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) { OnLineUtil.ONLINE_COUNT--; }}
LoginServlet
用户登陆后,接收参数并实现页面跳转
package com.hmc.listener.servlet;import com.hmc.listener.model.User;import javax.servlet.ServletException;import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** * User:Meice * 2017/10/11 *///@WebFilter(urlPatterns = "/*",// initParams = {// @WebInitParam(name = "encoding",value = "UTF-8")// })@WebServlet(urlPatterns = "/login.do")public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //接收参数 String gustName = req.getParameter("gustName"); //实例化User User user = new User(gustName); req.getSession().setAttribute("user",user); //页面跳转 resp.sendRedirect("online.jsp"); }}
LoginOutServlet
功能:点击离开,实现用户销毁
package com.hmc.listener.servlet;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 java.io.IOException;/** * User:Meice * 2017/10/11 */@WebServlet(urlPatterns = "/loginOut.do")public class LoginOutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //销毁session req.getSession().invalidate(); resp.sendRedirect("enter.jsp"); }}
OnLineUtil
就是用户统计的一个常量存放
package com.hmc.listener.util;/** * User:Meice * 2017/10/11 */public class OnLineUtil { //为便于调用且数量开始是常量所以定义为static public static int ONLINE_COUNT=0;}
结果是这样的:
原谅我没有更多的浏览器了。。。。。。
如果让很多人访问,场面一定很壮观!OK,到这里吧。下期再见!
阅读全文
0 0
- JavaWeb-Listener2-在线用户统计
- javaWeb 在线人数统计
- 在线用户统计
- 在线用户统计
- 在线用户统计
- 用户在线统计实现
- 统计在线用户
- 统计在线用户
- 统计用户在线列表
- 在线用户统计
- 在线登录用户统计
- 统计在线用户人数
- 统计在线用户 踢人
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- MacOS安装TensorFlow
- 全球顶级院士科学家来阿里巴巴,要搞什么大事?
- Android推送
- Andoroid进阶--底部图片选择Dialog
- java文件传输基础:OutputStream类及实现文件的复制
- JavaWeb-Listener2-在线用户统计
- 【JavaWeb】SpringMvc返回json
- 如何在去掉空数据及如何在matplotlib中显示数据线性拟合
- pg用户角色密码默认使用md5加密
- Excel 向程序发送命令时出现问题
- C3P0连接池
- mysql 链表查询(合计总数)
- dedecms中无法直接在arctype数据表中直接调取栏目链接
- pg(HGDB)在非默认位置创建表空间