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