Session 加 Cookie 实现自动登录【重点】
登录页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="login" method="post">
<table align="center">
<tr>
<td>姓名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="reset" value="重置">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
主页
/**
* 主页面
*/
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容编码集
resp.setContentType("text/html;charset=utf-8");
// 获取Session,注意一定要加false,不允许手动创建
HttpSession session = req.getSession(false);
// 判断:session为空,或者session中的username不为张三,说明没有登录
if (null == session || !"张三".equals(session.getAttribute("username"))) {
// 登录失败,重定向到登录界面
resp.sendRedirect("login.html");
} else {
// 登录成功,准备HTML页面并展示session中的数据
String html = "<h1 style='color : green'>" + "欢迎" + session.getAttribute("username") + "登录成功!!!</h1> ";
html += "<a href='logout'>退出登录</a>";
// 响应HTML页面
resp.getWriter().append(html);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
登录页
/**
* 登录操作,并且注册Session信息
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置请求编码集
req.setCharacterEncoding("UTF-8");
// 从前端获取参数
String username = req.getParameter("username");
String password = req.getParameter("password");
// 判断数据合法性
if ("张三".equals(username) && "123456".equals(password)) {
// 登录成功,创建session
HttpSession session = req.getSession(true);
// 设置过期时间
session.setMaxInactiveInterval(60 * 60);
// 设置登录到的用户名为参数
session.setAttribute("username", username);
// 创建Cookie并存入JSESSIONID
Cookie cookie = new Cookie("JSESSIONID", session.getId());
// 设置Cookie过期时间
cookie.setMaxAge(60 * 60);
// 通过响应对象将Cookie发送到浏览器
resp.addCookie(cookie);
// 重定向至主页
resp.sendRedirect("index");
} else {
// 登录失败,返回登录页重新登录
resp.sendRedirect("login.html");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
登出
/**
* 退出登录,销毁Session
*/
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取当前已存在的session,注意要用false
HttpSession session = req.getSession(false);
// 判断session是否为空
if (session != null) {
session.invalidate();
}
// 通过 JSESSIONID 获取 cookie 对象
Cookie cookie = new Cookie("JSESSIONID", "");
// 设置 cookie 的有效期为 0
cookie.setMaxAge(0);
// 发送Cookie数据至浏览器
resp.addCookie(cookie);
resp.sendRedirect("index");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
版权属于:不冷
本文链接:https://www.buleng.xyz/archives/101/
转载时须注明出处及本声明