Cookie
1.什么是cookie?
| Cookie是服务器通知客户端保存键值对的一种技术。 |
| 客户端有了Cookie后,每次请求都发送给服务器。 |
| 每个Cookie的大小不能超过4KB |
2.如何创建cookie?

可以同时创建多个cookie返回,并且一定要使用response.addCookie()方法返回cookie,否则客户端接收不到。
浏览器-检查-application-响应头 => set-cookie
Servlet程序中的代码:
| protected void createCookie(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| |
| Cookie cookie = new Cookie("key4","value4"); |
| |
| resp.addCookie(cookie); |
| |
| Cookie cookie1 = new Cookie("key5","value5"); |
| |
| resp.addCookie(cookie1); |
| resp.getWriter().write("Cookie创建成功"); |
| } |
3.服务器如何获取cookie
服务器获取客户端的Cookie只需要一行代码:req.getCookies():Cookie[ ]

Servlet程序中的代码:
| protected void getCookie(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| Cookie[] cookies = req.getCookies(); |
| for(Cookiecookie:cookies){ |
| |
| |
| resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]<br/>"); |
| } |
| |
| |
| Cookie iWantCookie = CookieUtils.findCookie("key1",cookies); |
| |
| if(iWantCookie != null){ |
| resp.getWriter().write("找到了需要的Cookie"); |
| } |
| } |
Cookie的工具类:
| public class CookieUtils{ |
| |
| |
| |
| |
| |
| |
| public static Cookie findCookie(String name,Cookie[]cookies){ |
| if(name == null || cookies == null || cookies.length == 0){ |
| return null; |
| } |
| for(Cookiecookie:cookies){ |
| if(name.equals(cookie.getName())){ |
| return cookie; |
| } |
| } |
| return null; |
| } |
| } |
4.cookie值的修改
| 方案一: |
| |
| |
| Cookie cookie = new Cookie("key1","newValue1"); |
| |
| resp.addCookie(cookie); |
| 方案二: |
| |
| Cookie cookie = CookieUtils.findCookie("key2",req.getCookies()); |
| |
| if(cookie != null){ |
| |
| cookie.setValue("newValue2"); |
| |
| resp.addCookie(cookie); |
| } |
5.浏览器如何查看cookie
谷歌浏览器如何查看Cookie:

火狐浏览器如何查看Cookie:

6.cookie的生命控制
| Cookie的生命控制指的是如何管理Cookie什么时候被销毁(删除) |
| |
| setMaxAge() |
| 正数,表示在指定的秒数后过期 |
| 负数,表示浏览器一关,Cookie就会被删除(默认值是-1) |
| 零,表示马上删除Cookie |
| |
| |
| |
| |
| |
| |
| |
| protected void life3600(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| Cookie cookie = new Cookie("life3600","life3600"); |
| cookie.setMaxAge(60*60); |
| resp.addCookie(cookie); |
| resp.getWriter().write("已经创建了一个存活一小时的Cookie"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| protected void deleteNow(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| |
| Cookie cookie = CookieUtils.findCookie("key4",req.getCookies()); |
| if(cookie != null){ |
| |
| cookie.setMaxAge(0); |
| |
| resp.addCookie(cookie); |
| resp.getWriter().write("key4的Cookie已经被删除"); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| protected void defaultLife(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| Cookie cookie = new Cookie("defalutLife","defaultLife"); |
| cookie.setMaxAge(-1); |
| resp.addCookie(cookie); |
| } |
7.cookie有效路径Path的设置
| Cookie的path属性可以有效的过滤哪些Cookie可以发送给服务器,哪些不发。 |
| path属性是通过请求的地址来进行有效的过滤。 |
| |
| CookieA path = /工程路径 |
| CookieB path = /工程路径/abc |
| |
| 请求地址如下: |
| http: |
| CookieA发送 |
| CookieB不发送 |
| |
| http: |
| CookieA发送 |
| CookieB发送 |
| protected void testPath(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| Cookie cookie = new Cookie("path1","path1"); |
| |
| cookie.setPath(req.getContextPath()+"/abc"); |
| resp.addCookie(cookie); |
| resp.getWriter().write("创建了一个带有Path路径的Cookie"); |
| } |
8.cookie练习-免输入用户名登录

| 说明: |
| 客户端第一次访问服务器,服务器返回login.jsp页面给客户端,客户端输入账号密码登录,服务器执行LogiinServlet程序,验证用户名与密码是否正确,正确则将获取到的用户名保存为Cookie发送给浏览器保存。 |
| |
| 客户端第二次访问服务器登录,会把cookie信息也发送给服务器,服务器根据cookie将响应的页面用户名设置好,实现免输入用户名。 |
login.jsp页面
| <form action="http://localhost:8080/13_cookie_session/loginServlet" method="get"> |
| 用户名:<input type="text" name="username" value="${cookie.username.value}"><br> |
| 密码:<input type="password" name="password"><br> |
| <input type="submit" value="登录"> |
| </form> |
LoginServlet程序:
| @Override |
| protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, |
| IOException{ |
| String username = req.getParameter("username"); |
| String password = req.getParameter("password"); |
| if("wzg168".equals(username) && "123456".equals(password)){ |
| |
| Cookie cookie = new Cookie("username",username); |
| cookie.setMaxAge(60*60*24*7); |
| resp.addCookie(cookie); |
| System.out.println("登录成功"); |
| }else{ |
| |
| System.out.println("登录失败"); |
| } |
| } |
Session会话
1.什么是session会话?
| 1、Session就一个接口(HttpSession)。 |
| 2、Session就是会话。它是用来维护一个客户端和服务器之间关联的一种技术。 |
| 3、每个客户端都有自己的一个Session会话。 |
| 4、Session会话中,我们经常用来保存用户登录之后的信息。session保存在服务器端。 |
2.Session创建和获取
| 如何创建和获取Session。它们的API是一样的。 |
| request.getSession() |
| 第一次调用是:创建Session会话 |
| 之后调用都是:获取前面创建好的Session会话对象。 |
| |
| isNew(); 判断到底是不是刚创建出来的(新的) |
| true表示刚创建 |
| false 表示获取之前创建 |
| |
| 每个会话都有一个身份证号。也就是ID值。而且这个ID是唯一的。 |
| getId() 得到Session的会话id值。 |
Session域数据的存取
| |
| |
| |
| |
| |
| |
| |
| protected void setAttribute(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| req.getSession().setAttribute("key1","value1"); |
| resp.getWriter().write("已经往Session中保存了数据"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| protected void getAttribute(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| Object attribute = req.getSession().getAttribute("key1"); |
| resp.getWriter().write("从Session中获取出key1的数据是:"+attribute); |
| } |
3.Session生命周期控制
| public void setMaxInactiveInterval(int interval) |
| 设置Session的超时时间(以秒为单位),超过指定的时长,Session就会被销毁。 |
| 值为正数的时候,设定Session的超时时长。 |
| 负数表示永不超时(极少使用) |
| public int getMaxInactiveInterval() 获取Session的超时时间 |
| public void invalidate() 让当前Session会话马上超时无效。 invalidate /ɪnˈvælɪ deɪt/ 使无效 |
| Session默认的超时时长是多少? |
| Session默认的超时时间长为30分钟。 |
| |
| 因为在Tomcat服务器的配置文件web.xml中默认有以下的配置,它就表示配置了当前Tomcat服务器下所有的Session |
| 超时配置默认时长为:30分钟。 |
| <session-config> |
| <session-timeout>30</session-timeout> |
| </session-config> |
| 如果说。你希望你的web工程,默认的Session的超时时长为其他时长。你可以在你自己的web.xml配置文件中做以上相同的配置。就可以修改你的web工程所有Seession的默认超时时长。 |
| |
| <session-config> |
| <session-timeout>20</session-timeout> |
| </session-config> |
| 如果你想只修改个别Session的超时时长。就可以使用上面的API。setMaxInactiveInterval(intinterval)来进行单独的设置。 |
| session.setMaxInactiveInterval(intinterval)单独设置超时时长。 |
Session超时的概念介绍:

| protected void life3(HttpServletRequest req,HttpServletResponse resp) throws ServletException, |
| IOException{ |
| |
| HttpSession session = req.getSession(); |
| |
| session.setMaxInactiveInterval(3); |
| resp.getWriter().write("当前Session已经设置为3秒后超时"); |
| } |
Session马上被超时示例:
| protected void deleteNow(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ |
| |
| HttpSession session = req.getSession(); |
| |
| session.invalidate(); |
| resp.getWriter().write("Session已经设置为超时(无效)"); |
| } |
4.浏览器和Session之间关联的技术内幕

删除掉Session的Cookie后,再次请求时,cookie中没有sessionId,服务器端不能再通过cookie找的sessionId,所以要重新创建一个session对象,并把sessionId保存到cookie中返回给浏览器。
session.getAttribute(key)拿到的是指针。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步