网页登录实现:设备二登录强迫已登录的用户下线

 1 package com.mvc.test;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.io.IOException;
 9 import java.io.PrintWriter;
10 import java.util.HashSet;
11 import java.util.Set;
12 
13 /**
14  * 用户是否登录,使用全局方法
15  *
16  * @author liuwenlong
17  * @create 2020-09-09 09:23:23
18  */
19 @SuppressWarnings("all")
20 @WebServlet(urlPatterns = "/login")
21 public class Application_Test extends HttpServlet {
22 
23 
24     @Override
25     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26         resp.setContentType("text/html;charset=UTF-8");
27         PrintWriter out = resp.getWriter();
28         String user = req.getParameter("user");
29         if (user == null) {
30             out.print("无效的用户或者密码!");
31             return;
32         }
33         String key = "islogin_" + user;
34         String islogin = (String) req.getServletContext().getAttribute(key);
35         if (islogin == null) {
36             if ("zs".equals(user) || "ls".equals(user)) {
37                 req.getServletContext().setAttribute(key + "id", req.getSession().getId());//记录SessionID
38                 req.getSession().setAttribute("islogin", user);
39                 req.getServletContext().setAttribute(key + "_t", System.currentTimeMillis());//记录时间
40 
41                 out.print("成功");
42             } else {
43                 out.print("无效的账号或者密码");
44             }
45         } else {
46             out.print("您已经登录...");
47         }
48     }
49 
50     //发心跳,接收新的心跳,如果关闭浏览器,就接收不到新的心跳,时间就会超时
51     @Override
52     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
53         resp.setContentType("text/html;charset=UTF-8");
54         PrintWriter out = resp.getWriter();
55         String user = req.getParameter("user");//取出用户
56         String key = "islogin_" + user;//这个用户,然后设置这个用户的时间,重新刷新时间
57         String id = (String) req.getServletContext().getAttribute(key + "id");
58         String id2 = req.getSession().getId();
59 
60         req.getServletContext().setAttribute(key + "_t", System.currentTimeMillis());//记录时间
61         if (id != id2) {
62             req.getSession().setAttribute("islogin", "notlogin");
63             out.print(user + "已经在设备" + id + "登录");
64         } else {
65             out.print("OK");
66         }
67     }
68 }

JSP页面:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>首页</title>
 5     <script type="text/javascript" src="static/js/jquery-3.3.1.min.js"></script>
 6 </head>
 7 <body>
 8 
 9 <font color="red" id="ts"></font>
10 
11 </body>
12 
13 <script type="text/javascript">
14     var inter = null;
15 
16     function heart() {
17         var islogin = "${sessionScope.islogin}";
18         if (islogin === "null" || islogin === "notlogin"||islogin.length===0) {
19             $("#ts").html("你还没有登录,请先登录");
20             return;
21         }else {
22             //$("#ts").html(islogin+"已经登录");
23             $.ajax({
24                 type:"post",
25                 url:"login",
26                 data:{user:islogin},
27                 success:function (res) { //回调函数
28                     $("#ts").html(res); //显示登录的返回信息
29                 }
30             })
31         }
32     }
33     setInterval("heart()", 3000);
34 </script>
35 </html>

原创文章,转载请说明出处,谢谢合作
原文地址:https://www.cnblogs.com/lwl80/p/13637982.html