JavaWeb学习六、Cookie与Session、Filter

十二、Cookie

12.1 介绍

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

12.2 创建

public void createCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1. 创建cookie对象
    Cookie cookie = new Cookie("key1", "value1");
    //2. 通知客户端保存cookie
    response.addCookie(cookie);
    response.getWriter().write("Cookie 创建成功");
}

12.3 服务器获取Cookie

Cookie工具类

package cn.imut.utils;

import javax.servlet.http.Cookie;

public class CookieUtils {
    public static Cookie findCookie(String name, Cookie[] cookies) {
        if(name == null || cookies == null || cookies.length == 0) {
            return null;
        }
        for (Cookie cookie: cookies) {
            if(name.equals(cookie.getName())){
                return cookie;
            }
        }
        return null;
    }
}
public void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    Cookie[] cookies = req.getCookies();
    // getName方法 返回 Cookie的 key(名)
    // getValue方法 返回 Cookie的 value值
    for (Cookie cookie : cookies) {
        resp.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "] <br/>");
    }

    Cookie iWantCookie = CookieUtils.findCookie("key1", cookies);

    //如果不等于null,说明赋过值,也就是找到了需要的Cookie
    if (iWantCookie != null) {
        resp.getWriter().write("找到了需要的 Cookie");
    }
}

12.4 Cookie值的修改

方案一、
  • 创建一个要修改的同名(指的就是 key)的 Cookie 对象
  • 在构造器,同时赋于新的 Cookie 值
  • 调用 response.addCookie(Cookie)
方案二、
  • 先查找到需要修改的 Cookie 对象
  • 调用 setValue()方法赋于新的 Cookie 值
  • 调用 response.addCookie()通知客户端保存修改

12.5 浏览器中查看Cookie

12.6 Cookie生命控制

Cookie 的生命控制指的是如何管理 Cookie 什么时候被销毁(删除)

setMaxAge()

  • 正数,表示在指定的秒数后过期
  • 负数,表示浏览器一关,Cookie 就会被删除(默认值是-1)
  • 零,表示马上删除 Cookie

十三、Session会话

13.1 介绍

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

13.2 创建Session和获取

request.getSession()

第一次调用是:创建 Session 会话

之后调用都是:获取前面创建好的 Session 会话对象。

isNew()判断到底是不是刚创建出来的(新的)

true 表示刚创建

false 表示获取之前创建

每个会话都有一个身份证号。也就是 ID 值。而且这个 ID 是唯一的。

getId() 得到 Session 的会话 id 值。

13.3 Session域数据的存取

public void serAttribute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getSession().setAttribute("key1", "value1");
    response.getWriter().write("向Session中保存了数据");
}

public void getAttribute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Object attribute = request.getSession().getAttribute("key1");
    response.getWriter().write("从Session中获取出key1的数据是:", + attribute);
}

13.4 Session生命周期控制

publicvoidsetMaxInactiveInterval(intinterval)

设置 Session 的超时时间(以秒为单位),超过指定的时长,Session 就会被销毁。

  • 值为正数的时候,设定 Session 的超时时长。
  • 负数表示永不超时(极少使用)

publicintgetMaxInactiveInterval(): 获取 Session 的超时时间

publicvoidinvalidate() : 让当前 Session 会话马上超时无效。

Session 默认的超时时间长为 30 分钟。

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

<session-config> 
    <session-timeout>30</session-timeout> 
</session-config

猜你喜欢

转载自www.cnblogs.com/yfyyy/p/12431904.html
今日推荐