简洁版用户登录系统

前端页面:

用户登录首页:

复制代码
<!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>

成果展示:

简洁版用户登录

相关推荐
不吃香菜学java5 小时前
Redis的java客户端
java·开发语言·spring boot·redis·缓存
码事漫谈5 小时前
大模型输出的“隐性结构塌缩”问题及对策
前端·后端
captain3765 小时前
事务___
java·数据库·mysql
北漂Zachary6 小时前
四大编程语言终极对比
android·java·php·laravel
小江的记录本6 小时前
【网络安全】《网络安全常见攻击与防御》(附:《六大攻击核心特性横向对比表》)
java·网络·人工智能·后端·python·安全·web安全
贵沫末6 小时前
python——打包自己的库并安装
开发语言·windows·python
文祐6 小时前
C++类之虚函数表及其内存布局(一个子类继承一个父类)
开发语言·c++
努力的小雨6 小时前
龙虾量化实战法(QClaw)
后端
zuowei28896 小时前
华为网络设备配置文件备份与恢复(上传、下载、导出,导入)
开发语言·华为·php
橙露6 小时前
SpringBoot 整合 MinIO:分布式文件存储上传下载
spring boot·分布式·后端