多用户在线人数监听(基于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>

效果图

后期会在加入其他功能(敬请期待!!!)

原文地址:https://www.cnblogs.com/Nick7/p/Nick.html