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已经没了

相关推荐
AskHarries几秒前
Java字节码增强库ByteBuddy
java·后端
小灰灰__20 分钟前
IDEA加载通义灵码插件及使用指南
java·ide·intellij-idea
夜雨翦春韭24 分钟前
Java中的动态代理
java·开发语言·aop·动态代理
程序媛小果44 分钟前
基于java+SpringBoot+Vue的宠物咖啡馆平台设计与实现
java·vue.js·spring boot
追风林1 小时前
mac m1 docker本地部署canal 监听mysql的binglog日志
java·docker·mac
芒果披萨1 小时前
El表达式和JSTL
java·el
duration~2 小时前
Maven随笔
java·maven
zmgst2 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
跃ZHD2 小时前
前后端分离,Jackson,Long精度丢失
java
blammmp2 小时前
Java:数据结构-枚举
java·开发语言·数据结构