首先写一个简陋的登陆页面 ----有记住我的复选框
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="do_login.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码 <input type="password" name="password"><br>
<input type="checkbox" name="rea" value="0">记住我<br/>
<input type="submit" value="登录">
</form>
</body>
</html>
再写do_login.jsp是逻辑判断检查用户是否勾选了"记住我".并设置有效期 ---判断登录成功与否并体跳转到不同的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
String[] dians = request.getParameterValues("rea");
boolean ia=false;
if (dians!=null && dians.length==1){
ia=true;
}
if ("curry".equals(username) && "30".equals(password)){
//设置用户登陆消息
session.setAttribute("username",username);
//设置session过期时间
session.setMaxInactiveInterval(60*60);
//检查用户是否自动登录
if (ia){
//如果选择自动登录,则设置Cookie并发送给客户端
Cookie cookie1=new Cookie("username",username);
Cookie cookie2=new Cookie("password",password);
//设置有效期为1天
cookie1.setMaxAge(60*60*24);
cookie2.setMaxAge(60*60*24);
response.addCookie(cookie1);
response.addCookie(cookie2);
}else{
//如果没有选择自动登录 则删除Cookie
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")){
cookie.setMaxAge(0);
response.addCookie(cookie);
}
if (cookie.getName().equals("password")){
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
//登陆成功后,打开用户首页
request.getRequestDispatcher("home.jsp").forward(request,response);
}else {
//登陆失败,提示重新登陆
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
如果登录失败,再次跳转到登陆也页面,登陆成功跳转到home.jsp页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
String[] dians = request.getParameterValues("rea");
boolean ia=false;
if (dians!=null && dians.length==1){
ia=true;
}
if ("curry".equals(username) && "30".equals(password)){
//设置用户登陆消息
session.setAttribute("username",username);
//设置session过期时间
session.setMaxInactiveInterval(60*60);
//检查用户是否自动登录
if (ia){
//如果选择自动登录,则设置Cookie并发送给客户端
Cookie cookie1=new Cookie("username",username);
Cookie cookie2=new Cookie("password",password);
//设置有效期为1天
cookie1.setMaxAge(60*60*24);
cookie2.setMaxAge(60*60*24);
response.addCookie(cookie1);
response.addCookie(cookie2);
}else{
//如果没有选择自动登录 则删除Cookie
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")){
cookie.setMaxAge(0);
response.addCookie(cookie);
}
if (cookie.getName().equals("password")){
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
//登陆成功后,打开用户首页
request.getRequestDispatcher("home.jsp").forward(request,response);
}else {
//登陆失败,提示重新登陆
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
注意:如果登陆时勾选了记住我 下一次可以直接访问home.jsp但是如果访问do_login.jsp会报错500,报错内容是空指针异常。