网站建设免费空间注册导航企业网站seo排名
Cookie和Session
出于安全考虑,浏览器不让网页直接操作文件系统,而Cookie就是一个折中的方案,可以让网页暂存一些数据在本地,不能存复杂的对象,只能存字符串。
Cookie是按照域名分类的,这个很好理解。
如何理解Cookie从服务器来,到服务器去?
来源:服务器将想进行存储的信息,通过字符串键值对的方式放到HTTP响应的Set-Cookie中
终点:服务器。客户端发送HTTP请求都会带上之前存储的Cookie信息(在HTTP的Header的Cookie中),让服务器去分析之前干了啥。
如何理解Cookie是在浏览器中工作的,session是在服务器这边工作的
识别用户信息的方式:
服务器直接通过Set-Cookie的方式返回用户信息给浏览器, 浏览器直接保存
服务器保存用户信息, 然后通过键值对进行保存. 其中键是由服务器(根据用户信息??)自动生成的唯一字符串, 值就是用户的详细信息. 服务器可以只把键(唯一字符串)通过Set-Cookie返回给浏览器.
两者的区别就是后者有点像是加密了, 后者将用户信息加密为键.
后面这样的处理方式, 就是会话方式. 键值对称之为session(会话), 唯一的字符串就称之为sessionId.
假如之前已经认证过信息,则再次登录流程如:
浏览器已有sessionId, 发送请求时候将sessionId一同发送给服务器
服务器根据接收的sessionId在哈希表中查找用户信息, 假如拿到了用户身份信息, 就认证成功, 反之失败.
服务器中用于存储用户信息的哈希表
以sessionId为key, 用户信息为value
每一个服务器中都会有很多个webapp,一个webapp对应一个存储session的哈希表,每一个哈希表的内容都是sessionId + session对象,每一个session对象中又可以又很多个键值对,如图:

创建一个前端页面:
<body><form action="login" method="post"><input type="text" name="Username"><input type="password" name="Password"><input type="submit" value="Submit"></form>
</body>
效果如:

点击Submit提交post请求后,后端处理登录信息:
查看用户名和密码输入格式,为空为null都要求重新输入
查看用户名和密码是否正确,不正确要求重新输入
@WebServlet("/login")
public class LoginServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String username = req.getParameter("Username");String password = req.getParameter("Password");if (username == null || username.equals("") || password == null || password.equals("")) {
// 信息有误,重定向到登录页面resp.sendRedirect("login.html");return;}// 用户名密码错误,重定向到登录页面if (!username.equals("Mattylyh") && !password.equals("222")) {resp.getWriter().write("Login failed, check your info before try again.");return;}// 登录信息正确,创建sessionHttpSession session = req.getSession(true);
// 将用户信息(键值对)写到session中session.setAttribute("username", "Mattylyh");
// 页面重定向到index初始页面 参数是相对路径resp.sendRedirect("index");}
}
验证信息成功后,
@WebServlet("/index")
public class IndexServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// index页面的作用就是将用户信息显示出来HttpSession session = req.getSession(false);if (session == null) {
// 登录失败就跳转登录页面resp.sendRedirect("login.html");return;}String username = session.getAttribute("Username");resp.setContentType("text/html; charset=utf8");resp.getWriter().write("当前用户:" + username);}
}
session和Cookie的协同作用主要是用来保持登录状态吧?
即第一次验证登录成功后,服务器给浏览器一个Set-Cookie,然后浏览器根据这个Set-Cookie的内容设置好Cookie。这个Set-Cookie和Cookie的内容都是一个JSESSIONID

有了这个JSESSIONID,之后每次访问服务器都会知道你是通过了登陆验证的,就不会需要你重复登录了。
但是这个管理会话的哈希表是在服务器内存中的,假如服务器重启了,那原理啊的哈希表也没了,就会需要重启会话。
改进成一个可看到访问次数(通过sessionId判断同一用户)的代码:
@WebServlet("/login")
public class LoginServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String username = req.getParameter("username");String password = req.getParameter("password");if (username == null || username.equals("") || password == null || password.equals("")) {
// 信息有误,重定向到登录页面resp.sendRedirect("login.html");return;}if (!username.equals("Mattylyh") || !password.equals("222")) {resp.getWriter().write("Login failed, check your info before try again.");return;}HttpSession session = req.getSession(true);
// 将用户信息(键值对)写到session中session.setAttribute("username", "Mattylyh");session.setAttribute("visitCount", 0);
// 页面重定向到index初始页面 参数是相对路径resp.sendRedirect("index");}
}
@WebServlet("/index")
public class IndexServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html; charset=utf8");
// index页面的作用就是将用户信息显示出来HttpSession session = req.getSession(false);if (session == null) {
// 登录失败就跳转登录页面resp.sendRedirect("login.html");return;}String username = String.valueOf(session.getAttribute("username"));Integer visitCount = (Integer) session.getAttribute("visitCount");visitCount += 1;session.setAttribute("visitCount", visitCount);resp.getWriter().write("当前用户:" + username + " 访问次数:" + visitCount);}
}