ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

利用cookie记住密码

2021-11-25 20:34:34  阅读:156  来源: 互联网

标签:username resp req 密码 cookie userpwd Cookie 记住


1.了解cookie(存储在浏览器)
HTTP 协议中的 Cookie 包括 Web Cookie 和浏览器 Cookie,它是服务器发送到 Web 浏览器的一小块数据。服务器发送到浏览器的 Cookie,浏览器会进行存储,并与下一个请求一起发送到服务器。通常,它用于判断两个请求是否来自于同一个浏览器,例如用户保持登录状态

2.html代码
拿取网址*链接:https://share.weiyun.com/SQ52HNUz 密码:zxy957
在这里插入图片描述

<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>智能健身房管理系统</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet">
    <link href="css/dashboard.css" rel="stylesheet">
    <link href="css/signin.css" rel="stylesheet">
    <script src="js/ie-emulation-modes-warning.js"></script>
    <script src="js/jquery-3.2.1.js"></script>
    <script src="js/jquery.cookie.js"></script>
    <script>
   
        //记住密码
        $(function () {
            var pwd = $.cookie("password");
            var username = $.cookie("username");
            $("#inputEmail").val($.cookie("username"));
            $("#inputPassword").val($.cookie("password"));
            if (username == null || username == "") {
                $("#reNaPw").prop("checked", false);
            }
            if (username != null && username != "") {
                $("#reNaPw").prop("checked", true);
            }
        });
    </script>
</head>

<body>
<div class="container">
    <form class="form-signin" id="path" method="post">
        <h2 class="form-signin-heading" style="text-align: center; color: darkorchid;">登入界面</h2>
        </br>
        <p>账户<input type="text" name="userid" id="inputEmail" class="form-control" placeholder="请输入账号" required
                    autofocus></p>
        <p>密码<input type="password" name="userpwd" id="inputPassword" class="form-control" placeholder="请输入账号密码"
                    required></p>
        <p>验证: <input type="text" style="width: 65px;height: 25px;border-radius: 13px" class="inputgri" name="number"/>
            <img id="num" src="resultNub"><a
                    href="javascript:"
                    onclick="document.getElementById('num').src = 'resultNub?'+(new Date()).getTime()">看不清</a>
        </p>
        <div class="checkbox">
            <label>
                <input type="checkbox" name="reNaPw" id="reNaPw" value="1"> 记住密码
                <select id="type">
                    <option value="0">请选登入用户类型</option>
                    <option value="1">用户</option>
                    <option value="2" selected>管理员</option>
                </select>
            </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" onclick="getPath()">登入</button>
    </form>

</div>
<script src="js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>

3.servlet 代码

    private void islogin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        System.out.println("path:" + path);//req请求路径
        String name = req.getParameter("userid");//根据input name属性得到登入表单的 id 数据
        String pwd = req.getParameter("userpwd");//得到登入表单的 id 数据
        HttpSession session = req.getSession();
        int flag = service.adminLogin(name, pwd);//访问数据的 admin  表判是否账号密码对应
        //number.equalsIgnoreCase(trueyzm) &&
        if (flag > 0) {
            System.out.println("管理员登入成功.....");
            session = req.getSession();
            //设置session-----欢迎:XX用户
            session.setAttribute("username", name);
            String reNaPw = req.getParameter("reNaPw");//记住密码按钮选择
            if (reNaPw == null || reNaPw.equals("")) {    //如果不记住密码就 清除 浏览器的 cookie
                Cookie username = new Cookie("username", "");
                Cookie userpwd = new Cookie("password", "");
                username.setPath("/");
                userpwd.setPath("/");
                resp.addCookie(username);
                resp.addCookie(userpwd);
            } else {
                //前端的记住密码的按钮的值
                if (reNaPw.equals("1")) {   //记住密码
                    Cookie username = new Cookie("username", name);
                    Cookie userpwd = new Cookie("password", pwd);
                    username.setMaxAge(60 * 60 * 24);//设置时间
                    userpwd.setMaxAge(60 * 60 * 24);
                    username.setPath("/");
                    userpwd.setPath("/");
                    resp.addCookie(username);
                    resp.addCookie(userpwd);
                }
            }
            resp.sendRedirect("/fitness/adminMenu.html");
            // req.getRequestDispatcher("/fitness/fitness/admin/adminMenu.html").forward(req, resp);
        } else {
            System.out.println("管理员登入失败.....");
            resp.sendRedirect("/fitness/index.html");
        }
    }

在这里插入图片描述

标签:username,resp,req,密码,cookie,userpwd,Cookie,记住
来源: https://blog.csdn.net/m0_45256755/article/details/121545987

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有