多用户在线人数监听(基于TomCat)
服务器Servlet端
package com.sxt.mvcpro.servlet;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OnlineNumberServlet extends HttpServlet{
private Set<String> names=new HashSet<>();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//编码修改为utf-8(需要中文显示不乱码必须设置)
req.setCharacterEncoding("utf-8");
//获取操作路径请求
String pathInfo=req.getPathInfo();
//登录请求
if ("/login".equals(pathInfo)) {
this.login(req,resp);
}
//登出请求
else if ("/logout".equals(pathInfo)) {
this.logout(req,resp);
}
}
//实现登录统计的方法
public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
//如果为null则表示还没有登录
if (req.getSession().getAttribute("username")==null) {
if (!"".equals(username)) {
//只有输入的用户名不能为空字符串才能进行操作
req.getSession().setAttribute("username", username);
//将用户名保存到set集合中
names.add(username);
//再将names集合保存到application内置对象中
req.getServletContext().setAttribute("users", names);
//集合大小即为人数多少
req.getServletContext().setAttribute("count", names.size());
}
}
//继续跳转到在线显示的页面
resp.sendRedirect("/Mvcpro/pages/online.jsp");
}
//实现注销的方法
public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
//移除当前用户输入的用户名
names.remove(req.getSession().getAttribute("username"));
//销毁当前用户的session内置对象
req.getSession().invalidate();
req.getServletContext().setAttribute("count", names.size());
resp.sendRedirect("/Mvcpro/pages/online.jsp");
} @Override
//通过doPost来调用doGet方法可以使账号不在地址栏显示
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
} }
TomCat配置文件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>Mvcpro</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--容器中配置出 路径对应的servlet-->
<servlet>
<servlet-name>lineNumberServlet</servlet-name>
<servlet-class>com.sxt.mvcpro.servlet.OnlineNumberServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--定义出上面的servlet处理路径 这个路径叫做servlet的映射路径-->
<servlet-mapping>
<servlet-name> lineNumberServlet</servlet-name>
<url-pattern>/online/*</url-pattern> </servlet-mapping>
</web-app>
客户端(前端)
由于需要传动态参数所以采用jsp格式而不是html(注意编码统一为utf-8以防止乱码)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<base href="/Mvcpro/">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>在线人数为:${count==null? 0:count}</h1>
${users}
<form action="online/login" method="post">
<fieldset>
<legend>请登录</legend>
用户名:<input type="text" name="username"><br><br>
<input type="submit" value="登陆">
</fieldset>
</form>
<a href="online/logout">注销</a>
</body>
</html>
效果图
后期会在加入其他功能(敬请期待!!!)
多用户在线人数监听(基于TomCat)的更多相关文章
- 基于tomcat插件的maven多模块工程热部署(附插件源码)
内容属原创,转载请注明出处 写在前面的话 最近一直比较纠结,归根结底在于工程的模块化拆分.以前也干过这事,但是一直对以前的结果不满意,这会重操旧业,希望搞出个自己满意的结果. 之前有什么不满意的呢? ...
- 简单读!tomcat源码(一)启动与监听
tomcat 作为知名的web容器,很棒! 本文简单了从其应用命令开始拆解,让我们对他有清晰的了解,揭开神秘的面纱!(冗长的代码流水线,给你一目了然) 话分两头: 1. tomcat是如何启动的? 2 ...
- android事件处理之基于监听
Android提供了了两种事件处理方式:基于回调和基于监听. 基于监听: 监听涉及事件源,事件,事件监听器.用注册监听器的方法将某个监听器注册到事件源上,就可以对发生在事件源上的时间进行监听. 最简单 ...
- tomcat触发ServletContext初始化监听事件的源码(原创)
tomcat 8.0.36 知识点: 动态监听器的好处可以根据环境条件进行选择性添加. 静态监听器有七类. ServletContextAttributeListener ServletRequest ...
- 解决tomcat启动Socket监听端口死循环被hold问题
原文链接:http://blog.csdn.net/dead_cicle/article/details/7073433 1.SOCKET监听置于servlet的init方法中,在web.xml里加入 ...
- JavaWeb学习记录(二十六)——在线人数统计HttpSessionListener监听实现
一.session销毁控制层代码 public class InvalidateSession extends HttpServlet { public void doGet(HttpServletR ...
- Android的事件处理机制详解(二)-----基于监听的事件处理机制
基于监听的事件处理机制 前言: 我们开发的app更多的时候是需要与用户的交互----即对用户的操作进行响应 这就涉及到了android的事件处理机制; android给我们提供了两套功能强大的处理机制 ...
- 普通用户从非80端口启动tomcat,通过端口转发监听80端口
linux下小于1024的端口都需要root去绑定. root权限启动tomcat是不明智的,可以使用非root权限启动tomcat监听8080端口,然后利用端口转发实现对80端口的监听. 端口转发: ...
- 监听tomcat服务器启动/关闭并从配置文件中读取参数进行初始化
监听tomcat服务器启动/关闭很简单(2步): 1. 建立一个类实现ServletContextListener接口,重写其中的方法(contextDestroyed和contextInitiali ...
随机推荐
- Linux日志文件总管——logrotate
日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处理一个单个的庞大日 ...
- Element ui 使用 Tree 树形控件
使用树形控件需要映入 jsx才能运行链接:https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage npm install\ babe ...
- Eclipse创建Dynamic Web部署
Eclipse创建Dynamic Web部署 http://blog.csdn.net/sweblish/article/details/6686046 Eclipse3.x中热部署项目,启动错误问题 ...
- 使用PostSharp在.NET平台上实现AOP(转)
出处:https://www.cnblogs.com/leoo2sk/archive/2010/11/30/aop-postsharp.html 摘要 本文首先介绍AOP(面向方面编程)的相关概念及理 ...
- Le Chapitre VIII
J'appris bien vite à mieux connaître cette fleur. Il y avait toujours eu, sur la planète du petit pr ...
- hibernate添加数据报错:Could not execute JDBC batch update
报错如下图所示: 报错原因:在配置文件或注解里设置了字段关联,但数据却没有关联. 解决方法:我的错误是向一个多对多的关联表里插入数据,由于表中一个字段的数据是从另一张表里get到的,通过调试发现,从以 ...
- Netty4.x 源码实战系列(一): 深入理解ServerBootstrap 与 Bootstrap
转载自:https://www.cnblogs.com/itdriver/p/8149913.html 从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统s ...
- Docker Compose部署 nginx代理Tomcat集群
一.简介 使用Docker镜像部署Nginx代理的多个Tomcat集群: 使用Dockerfile构建nginx镜像 使用Dockerfile构建tomcat镜像 mysql镜像使用docker hu ...
- MIT Molecular Biology 笔记3 DNA同源重组
视频 https://www.bilibili.com/video/av7973580?from=search&seid=16993146754254492690 教材 Molecular ...
- Fortran编译器之一GUN Fortran安装(Windows XP)
最近研究GIS算法,需要用到Fortran语言.在网上找了一下发现一个开源的软件GUN Fortran编译器.当然既然是编译器,就是编译出程序的,但是编辑器不包括在内.编辑器可以用Text记事本,或者 ...