| |
| 配⼀个唯⼀标识ID和response⼀并返回给浏览器 |
| |
| |
| 务会获取这个标识ID去map⾥⾯找上⼀次request的信息状态且做对应的更新操作 服务 |
| 端⽣成这个全局的唯⼀标识,传递给客户端⽤于标记这次请求就是cookie; 服务器创建 |
| 的那个map结构就是session |
| |
| 送 |
| |
| 核⼼:它⽤于告知服务端两个请求是否来⾃同⼀浏览器,如保持⽤户的登录状态 |
| |
| 点的cookie |
| Name : 名称 |
| Value : 值 |
| Domain:表示当前cookie所属于哪个域或⼦域下⾯ |
| Expires/Max-age:表示了cookie的有效期,是⼀个时间,过了这个时间,该cookie就失效了 |
| Path:表示cookie的所属路径。 |
| size: ⼤⼩,多数浏览器都是4000多个字节 |
| http-only: 表示这个cookie不能被客户端使⽤js读取到,是不公开的cookie,(chrome调试器的console中输⼊document.cookie将得不到标记为HttpOnly的字段) |
| Secure: 标记为 Secure 的Cookie只应通过被HTTPS协议加密过的请求发送给服务端, 从 Chrome 52 和 Firefox 52 开始,不安全的站点(http:)⽆法使⽤Cookie的 Secure标记 |
| SameSite(特有的,可以忽略) |

| cookie会被附加在每个HTTP请求中,增加了流量。 |
| 在HTTP请求中的cookie是明⽂传递的,所以安全性成问题,除⾮⽤HTTPS |
| Cookie的⼤⼩有限制,对于复杂的存储需求来说不满⾜ |
| 多数浏览器允许最多是50个,部分浏览器是30或者20; |
| 满后会有多种剔除策略,⽐如LRU,权重等 |
| 客户端第1次请求服务端时,服务端会返回cookie和response |
| 客户端将cookie存储起来 |
| 客户端第2次及以后每次访问服务端都会带上cookie |
| @WebServlet("/get_cookie") |
| public class GetCookieServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| Cookie[] cookies = request.getCookies(); |
| for(Cookie cookie : cookies){ |
| System.out.println(cookie.getDomain()); |
| System.out.println(cookie.getName()); |
| System.out.println(cookie.getValue()); |
| } |
| } |
| |
| } |
- 测试,控制台清空后访问,访问后返回cookie

| http://localhost:8080/get_cookie |
| |
| |
| null |
| JSESSIONID |
| FF39D4A8A5EBC89F25969E1BD4FC602A |
| @WebServlet("/gene_cookie") |
| public class GeneCookieServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| |
| Cookie cookie = new Cookie("token","sfwerawefewadaewfafewafa"); |
| |
| cookie.setMaxAge(20); |
| |
| response.addCookie(cookie); |
| |
| request.getRequestDispatcher("/index.jsp").forward(request,response); |
| } |
| |
| } |
| http://localhost:8080/gene_cookie |

| |
| 分配⼀个唯⼀标识sessionId和response⼀并返回给浏览器 |
| |
| |
| sessionId |
| |
| |
| 注意: Session是有时限性的:⽐如如果30分钟内某个session都没有被更新,服务器就会删 |
| 除这个它 |
| 服务端⽣成这个全局的唯⼀标识,传递给客户端⽤于标记这次请求就是cookie |
| 服务器创建的那个map结构就是session |
| cookies由服务端⽣成,⽤于标记客户端的唯⼀标识,在每次⽹络请求中,都会被传送。 |
| session服务端⾃⼰维护的⼀个map数据结构,记录key-Object上下⽂内容状态 |
| 总⾔之cookie是保存在客户端,session是存在服务器,session依赖于cookie |
| cookie⾥⾯存储的就是JSESSIONID |
| # session是存储在服务端的内存中,在javaweb⾥⾯叫HttpSession也是⼀个作⽤域 |
| # PageContext(⻚⾯)->ServletRequest(请求)->【HttpSession】(会话)->ServletContext(⼀个 |
| 应⽤) |
| # 是可以存储很多key-value的,作⽤域⽐较⼴,所以也不能存储过多内容,因为内存是有限制 |
| 的,互联⽹企业使⽤⽐较少,传统IT公司使⽤⽐较多 |
| # 创建SessionServlet |
| |
| @WebServlet("/session") |
| public class SessionServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| |
| HttpSession session = request.getSession(); |
| |
| System.out.println("sessionid="+session.getId()); |
| |
| System.out.println("getCreationTime="+session.getCreationTime()); |
| |
| System.out.println("isNew="+session.isNew()); |
| |
| session.setAttribute("name","goudan"); |
| } |
| |
| } |
- 启动项目测试
- 清空session

- 第1次访问,返回sessionid

| |
| sessionid=8BFC3D3140B3539B18DF1348EBC369ED |
| getCreationTime=1649860447633 |
| isNew=true |
- 第2次访问

| |
| sessionid=8BFC3D3140B3539B18DF1348EBC369ED |
| getCreationTime=1649860447633 |
| isNew=false |
| # 编写LoginServlet |
| @WebServlet("/loginServlet") |
| public class LoginServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| this.doPost(req,resp); |
| } |
| |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| |
| String name = req.getParameter("name"); |
| String pwd = req.getParameter("pwd"); |
| |
| if(name.equals("user") && pwd.equals("123")){ |
| User user = new User(); |
| user.setId(121); |
| user.setName(name); |
| user.setHost("xdclass.net"); |
| req.getSession().setAttribute("loginUser",user); |
| |
| req.getRequestDispatcher("/WEB-INF/user.jsp").forward(req,resp); |
| }else{ |
| |
| req.setAttribute("msg","账号密码错误"); |
| req.getRequestDispatcher("/login.jsp").forward(req,resp); |
| } |
| } |
| |
| } |
| |
| |
| # 编写LogoutServlet |
| @WebServlet("/logout_servlet") |
| public class LogoutServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| |
| HttpSession session = request.getSession(); |
| session.invalidate(); |
| request.getRequestDispatcher("/login.jsp").forward(request,response); |
| } |
| |
| } |
| |
| |
| # 编写登录成功的页面,user.jsp |
| <body> |
| id:${loginUser.id} |
| <br> |
| name:${loginUser.name} |
| <a href="/logout_servlet">退出</a> |
| </body> |
| |
| |
| # 编写登录页面,login.jsp |
| <body> |
| <form action="<%=request.getContextPath()%>/loginServlet" method="post"> |
| 名称:<input type="text" name="name"/> |
| <br/> |
| 密码:<input type="password" name="pwd"/> |
| <input type="submit" value="登录"> |
| 消息提示 ${msg} |
| </form> |
| </body> |
| |
| |
| # web.xml中设置session超时时间 |
| <session-config> |
| <session-timeout>30</session-timeout> |
| </session-config> |
| # 客户端首次访问服务器时,服务器生成sessionid,将sessionid保存在cookie中发送个客户端;服务端则将sessionid保存在session中,记录了这个请求与服务端的关系 |
| # 客户端再次访问服务端时,就会带上cookie,服务端拿到cookie中的sessionid,与session中id比较,一致则允许访问 |
| # 当服务端的session失效时,则会生成新的sessionid返回给客户端 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术