简洁版用户登录系统

前端页面:

用户登录首页:

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

成果展示:

简洁版用户登录

相关推荐
SmoothSailingT几秒前
C#——textBox控件(1)
开发语言·c#
Echo flower2 分钟前
Spring Boot WebFlux 实现流式数据传输与断点续传
java·spring boot·后端
没有bug.的程序员8 分钟前
微服务中的数据一致性困局
java·jvm·微服务·架构·wpf·电商
鸽鸽程序猿12 分钟前
【Redis】Java客户端使用Redis
java·redis·github
悦悦子a啊12 分钟前
使用 Java 集合类中的 LinkedList 模拟栈以此判断字符串是否是回文
java·开发语言
Lucky小小吴14 分钟前
java代码审计入门篇——Hello-Java-Sec(完结)
java·开发语言
一个想打拳的程序员16 分钟前
无需复杂配置!用%20docker-webtop%20打造跨设备通用%20Linux%20桌面,加载cpolar远程访问就这么简单
java·人工智能·docker·容器
一起养小猫19 分钟前
LeetCode100天Day2-验证回文串与接雨水
java·leetcode
csbysj202021 分钟前
XML 技术
开发语言
清晓粼溪22 分钟前
Java登录认证解决方案
java·开发语言