Login.jsp
<body>
<div class="container">
<form class="form-signin" action="LoginCheck" method="post">
<h2 class="form-signin-heading">登录 </h2>
<input type="text" id="inputEmail"
class="form-control" placeholder="请输入手机号"
required autofocus name="uID" value="${ID}">
<label for="inputPassword" class="sr-only" >
请输入密码
</label>
<input type="password" id="inputPassword"
class="form-control" placeholder="请输入密码"
required name="upsw" value="${psw}">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" value="rememberme"> 记住我的账号与密码
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">登录</button>
</form>
</div> <!-- /container -->
<%
String uID = null;
String upwd = null;
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("userinfo")) {
//存着数据(用户名+密码)
uID = cookies[i].getValue().split("-")[0];
upwd = cookies[i].getValue().split("-")[1];
//再一次的存起来(备用)
request.setAttribute("ID", uID);
request.setAttribute("psw", upwd);
}
}
%>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.8/assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
LoginCheck
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
//获取用户账号、是否保留?
String uID=request.getParameter("uID");
String upsw=request.getParameter("upsw");
String remember=request.getParameter("remember");
//out.println(uID);
//out.println(upsw);
//out.println(remember);
//登录验证
UserDao dao=new UserDao();
beans.User u=new User();
u.setUserID(uID);
u.setUserpsw(upsw);
int i=dao.loginValidate(u);
//有此用户
if(i==1){
//out.println("success!");
//request.setAttribute("userID", uID);
//request.setAttribute("userpsw", upsw);
if("rememberme".equals(remember)){
// 构造Cookie对象
// 添加到Cookie中
Cookie cookie = new Cookie("userinfo", uID + "-" + upsw);
// 设置过期时间
cookie.setMaxAge(60*60*24);
// 存储
response.addCookie(cookie);
}
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
else{
//request.getRequestDispatcher("/Login.jsp?error=yes").forward(request, response);
out.print("error");
}
}
再次返回登录界面依然是没有账号和密码的