简洁版用户登录系统

前端页面:

用户登录首页:

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

成果展示:

简洁版用户登录

相关推荐
GetcharZp2 小时前
GitHub 49K+ Star!C++ 开发者必知的 JSON 神级库:从零到精通全指北
后端
fqbqrr2 小时前
2606C++,C++构的多态
开发语言·c++
xujinwei_gingko2 小时前
SpringBoot整合WebSocket
spring boot·后端·websocket
智码看视界2 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
程序员cxuan2 小时前
Claude Fable 5 来了
人工智能·后端·程序员
biter down3 小时前
从 0 到 1 搭建 Python 接口自动化测试框架(博客系统实战)
开发语言·python
JS菌3 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
wang09073 小时前
自己动手写一个spring之IOC_2
java·后端·spring
来杯@Java3 小时前
学生选课管理系统(基于springboot+vue前后端分离的项目)计算机毕业设计java
java·spring boot·spring·vue·毕业设计·maven·mybatis