cookie记住密码、session欢迎用户登入

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");
        }
    }

在这里插入图片描述
4sission(存在服务器)
会话 , Session是基于Cookie的一种会话机制。 Cookie是服务器返回一小份数据给客户端,并且存放在客户端上。 Session是,数据存放在服务器端。

4.1登入页面的servlet存取session 的值

 String name = req.getParameter("userid");//得到登入表单的 id 数据
  HttpSession session = req.getSession();//获取session 对象..
 //设置session-----欢迎:XX用户
 session.setAttribute("username", name);

*4.2登入成功页面一进入就发ajax拿取session

        //欢迎用户
        $(function () {
            $.getJSON("/fitness/admin/count.do", function (data) {
             /*在前端页面拼写数据*/
                $("#user").text("歡迎:" + data.session);
            });
        });

*4.2登入成功页面sevlet

    JSONObject jsonObject = new JSONObject();//穿件JSONObject对象
     String uname = req.getSession().getAttribute("username").toString();// 拿session的
     jsonObject.put("session", uname);//存到回调函数,返回给客户端
          //服务端获取到输出对象,
        PrintWriter out = resp.getWriter();
        //通过输出对象把字符串写出到客户端
        out.print(jsonObject);

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值