简洁版用户登录系统

前端页面:

用户登录首页:

复制代码
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户登录首页</title>
</head>

<body>
    登录人: <span id="loginUser"></span>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script>
        
    </script>
</body>

</html>

登录页面:

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>登录页面</title>
</head>

<body>
  <h1>用户登录</h1>
  用户名:<input name="userName" type="text" id="userName"><br>
  密码:<input name="password" type="password" id="password"><br>
  <input type="button" value="登录" onclick="login()">
  
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  <script>
    function login() {
    
    }

  </script>
</body>

</html>

后端代码:

复制代码
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public boolean login(String userName, String password , HttpSession session){

        if (userName==null || userName.length()==0 || password==null || password.length()==0){
            return false;
        }
        if ("admin".equals(userName) && "admin".equals(password)){
            //设置session
            session.setAttribute("username","admin");
            return true;
        }
        return false;

    }

    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpServletRequest request){
        //从session获取登录用户
        HttpSession session = request.getSession(false);
        String userName=null;
        if (session !=null){
            userName = (String)session.getAttribute("username");
        }
        return "当前登录用户:"+userName;
    }
}

前端代码:

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>

<body>
<h1>用户登录</h1>
用户名:<input name="userName" type="text" id="userName"><br>
密码:<input name="password" type="password" id="password"><br>
<input type="button" value="登录" onclick="login()">

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
    function login() {
      console.log("登录")
      $.ajax({
        url:"user/login",
        type:"post",
        data:{
          "userName":$("#userName").val(),
          "password":$("#password").val()
        },

        success:function(result){
          if(result){
            location.href="/index.html";
            //location.assign();
          }else{
            alert("密码错误!");
          }
        }
      });
    }

  </script>
</body>

</html>


<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户登录首页</title>
</head>

<body>
登录人: <span id="loginUser"></span>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
    //页面加载时,就去调用后端请求
    $.ajax({
        url:"/user/getUserInfo",
        type:"get",
        success:function(username){
            $("#loginUser").text(username);
        }
    });

</script>
</body>

</html>

成果展示:

简洁版用户登录

相关推荐
不绝1915 分钟前
C#核心——面向对象:封装
开发语言·javascript·c#
没有bug.的程序员9 分钟前
HashMap 源码深度剖析:红黑树转换机制与高并发性能陷阱
java·性能优化·并发编程·源码分析·红黑树·hashmap·技术深度
yaoxin52112319 分钟前
294. Java Stream API - 对流进行归约
java·开发语言
ghie909019 分钟前
基于MATLAB的演化博弈仿真实现
开发语言·matlab
曹轲恒19 分钟前
Thread.sleep() 方法详解
java·开发语言
aini_lovee21 分钟前
基于Qt实现CAN通信上位机
开发语言·qt
Coder_Boy_22 分钟前
基于SpringAI的在线考试系统-考试模块前端页面交互设计及优化
java·数据库·人工智能·spring boot
小小仙。25 分钟前
IT自学第十九天
java·开发语言
悟空码字27 分钟前
SpringBoot集成Hera,分布式应用监控与追踪解决方案
java·springboot·编程技术·后端开发·hera
砚边数影30 分钟前
Java基础强化(三):多线程并发 —— AI 数据批量读取性能优化
java·数据库·人工智能·ai·性能优化·ai编程