Cookie和Session详解

一、Cookie

1. 什么是Cookie

  • Cookie是服务器通知客户端保存键值对的一种技术
  • 客户端有了Cookie后,每次请求都发送给服务器
  • 每次Cookie的大小不能超过4kb

2. 如何创建Cookie

在这里插入图片描述


		Cookie cookie = new Cookie("key1","value1");
        resp.addCookie(cookie);

3. 服务器如何获取Cookie

在这里插入图片描述

Cookie[] cookies = req.getCookies();

4. Cookie值的修改

4.1 方案一

  • 先创建一个要修改的同名的Cookie对象
  • 在构造器,同时赋予新的Cookie值
  • 调用response,addCookie(cookie)
	Cookie cookie = new Cookie("key1","newValue1"); 
	resp.addCookie(cookie);

4.2 方案二

  • 先查找到需要修改的 Cookie 对象
  • 调用 setValue()方法赋于新的 Cookie 值
  • 调用 response.addCookie()通知客户端保存修改
	Cookie cookie = CookieUtils.findCookie("key2",req.getCookies());
	if (cookie != null) {
		cookie.setValue("newValue2");
 		resp.addCookie(cookie); 
	 }

5. Cookie生命控制

  • Cookie的生命控制指的是如何管理Cookie什么时候被销毁
  • setMaxAge()
  1. 正数,表示在指定的秒数后过期
  2. 负数,表示浏览器一关,Cookie就会被删除(默认值是-1)
  3. 零,表示马上删除Cookie

6. 免输入用户名登录

在这里插入图片描述

  • 创建Servlet

public class CookieServlet 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 username = req.getParameter("username");
        String password = req.getParameter("password");
        if("lisi".equals(username) && "123456".equals(password)){
            Cookie cookie = new Cookie("name",username);
            cookie.setMaxAge(60*60*24*7);
            resp.addCookie(cookie);
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
    }
}
  • 创建jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/cookieServlet">
        username:<input type="text" name="username" value="${cookie.name.value}"><br>
        password:<input type="password" name="password"><br>
        <input type="submit">
    </form>
</body>
</html>
  • 运行第一次需要都输入,进行创建Cookie,第二次进入,用户名就不需要再进行输入
    在这里插入图片描述

二、Session会话

1. 什么是Session会话

  • Session 就一个接口
  • Session 就是会话,它是用来维护一个客户端和服务器之间关联的一种技术
  • 每个客户端都有自己的一个 Session 会话
  • Session 会话中,我们经常用来保存用户登录之后的信息

2. 如何创建Session和获取(id号,是否为新)

  • 如何创建和获取 Session,它们的 API 是一样的。
  • request.getSession()
  1. 第一次调用是:创建 Session 会话
  2. 之后调用都是:获取前面创建好的 Session 会话对象
  • isNew(); 判断到底是不是刚创建出来的

true 表示刚创建
false 表示获取之前创建

  • 每个会话都有一个身份证号,也就是id值,而且这个id是唯一的
  • getid() 得到Session的会话id值

3. Session域数据的存取

  • 保存数据
	req.getSession().setAttribute("key1", "value1"); 
	resp.getWriter().write("已经往 Session 中保存了数据");
  • 读取数据
	Object attribute = req.getSession().getAttribute("key1"); 
	resp.getWriter().write("从 Session 中获取出 key1 的数据是:" + attribute);

4. Session生命周期控制

  • public void setMaxInactiveInterval(int interval)
  • 设置 Session 的超时时间(以秒为单位),超过指定的时长,Session 就会被销毁
  1. 值为正数的时候,设定 Session 的超时时长
  2. 负数表示永不超时
  • public int getMaxInactiveInterval()获取 Session 的超时时间
  • public void invalidate() 让当前 Session 会话马上超时无效

4.1 Session 默认的超时时长是多少

  • Session 默认的超时时间长为 30 分钟
  • 因为在 Tomcat 服务器的配置文件 web.xml中默认有以下的配置,它就表示配置了当前 Tomcat 服务器下所有的 Session 超时配置默认时长为:30 分钟。
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>

4.2 Session 超时的概念

  • session的超时指的是,客户端两次请求的最大时长

5. 浏览器和 Session 之间关联的技术内幕

  • Session 技术,底层其实是基于 Cookie 技术来实现的
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值