Cookie的详解使用(创建,获取,销毁)

文章目录

Cookie的详解使用(创建,获取,销毁)

1、Cookie是什么

  1. cookie是在浏览器中保存的

  2. 如果想要使用cookie要保证我们的浏览器是开启cookie,所以说有一定的弊端,如果浏览器没有开启cookie,就不能再使用cookie了

  3. cookie的大小是有限制的,通常是4096byte

  4. cookie的保存是以键值对的形式存在的

2、cookie的常用方法

java 复制代码
//1.cookie的构造方法,目的是实例化出来cookie对象
Cookie(String name,String value)
//2.设置cookie的方法
setValue(String value) //修改cookie的值
getValue(String value) //获得cookie的值    
getName(String value) //获得cookie的键    
setMaxAge(int time) //设置cookie的有效时间
//3.要将cookie发送到浏览器
response.addCookie(Cookie cookie);
//4.获得所有cookie
request.getCookies();

3、cookie的构造和获取代码演示

SetCookieServlet.java

java 复制代码
/*
 * Copyright (c) 2020, 2023, fpl1116.cn All rights reserved.
 *
 */
package cn.fpl.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>Project: JavaWebDemo - SetCookieServlet</p>
 * <p>Powered by fpl1116 On 2023-12-27 09:50:33</p>
 * <p>描述:<p>
 *
 * @author fpl1116 [2391940642@qq.com]
 * @version 1.0
 * @since 1.8
 */
public class SetCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建cookie对象
        Cookie cookie = new Cookie("fpl","fpl1116");

        //设置过期时间
        cookie.setMaxAge(60*60*24);

        //发送浏览器响应
        resp.addCookie(cookie);

    }
}

GetCookieServlet.java

java 复制代码
/*
 * Copyright (c) 2020, 2023, fpl1116.cn All rights reserved.
 *
 */
package cn.fpl.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * <p>Project: JavaWebDemo - SetCookieServlet</p>
 * <p>Powered by fpl1116 On 2023-12-27 09:50:33</p>
 * <p>描述:<p>
 *
 * @author fpl1116 [2391940642@qq.com]
 * @version 1.0
 * @since 1.8
 */
public class GetCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        for(Cookie cookie : cookies){
            System.out.println("=============");
            System.out.println(cookie.getName());
            System.out.println(cookie.getValue());
        }


    }
}

web.xml

java 复制代码
<servlet>
        <servlet-name>setcookie</servlet-name>
        <servlet-class>cn.fpl.servlet.SetCookieServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>setcookie</servlet-name>
        <url-pattern>/setcookie</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>getcookie</servlet-name>
        <servlet-class>cn.fpl.servlet.GetCookieServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>getcookie</servlet-name>
        <url-pattern>/getcookie</url-pattern>
    </servlet-mapping>

运行结果如下

setcookie

getcookie

4、Cookie的销毁

DestoryCookieServlet

java 复制代码
package cn.fpl.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DestoryCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得所有cookie
        Cookie[] cookieArr = req.getCookies();
        for (Cookie cookie : cookieArr) {
            if("fpl".equals(cookie.getName())){
                //修改fpl的失效时间
                cookie.setMaxAge(0);
                resp.addCookie(cookie);
            }
        }
    }
}

web.xml

java 复制代码
<servlet>
        <servlet-name>destoryCookie</servlet-name>
        <servlet-class>cn.fpl.servlet.DestoryCookieServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>destoryCookie</servlet-name>
        <url-pattern>/destoryCookie</url-pattern>
    </servlet-mapping>

运行结果如下,刚才设置的cookie已经没了

相关推荐
gb42152871 分钟前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶2 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
zfoo-framework10 分钟前
【jenkins插件】
java
风_流沙15 分钟前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch
ProtonBase44 分钟前
如何从 0 到 1 ,打造全新一代分布式数据架构
java·网络·数据库·数据仓库·分布式·云原生·架构
乐之者v1 小时前
leetCode43.字符串相乘
java·数据结构·算法
suweijie7684 小时前
SpringCloudAlibaba | Sentinel从基础到进阶
java·大数据·sentinel
公贵买其鹿5 小时前
List深拷贝后,数据还是被串改
java
xlsw_8 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis