文章目录
- Cookie的详解使用(创建,获取,销毁)
- 1、Cookie是什么
- 2、cookie的常用方法
- 3、cookie的构造和获取代码演示
- SetCookieServlet.java
- GetCookieServlet.java
- web.xml
- 运行结果如下
- 4、Cookie的销毁
- DestoryCookieServlet
- web.xml
- 运行结果如下,刚才设置的cookie已经没了
Cookie的详解使用(创建,获取,销毁)
1、Cookie是什么
cookie是在浏览器中保存的
如果想要使用cookie要保证我们的浏览器是开启cookie,所以说有一定的弊端,如果浏览器没有开启cookie,就不能再使用cookie了
cookie的大小是有限制的,通常是4096byte
cookie的保存是以键值对的形式存在的
2、cookie的常用方法
//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
/** 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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected 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
/** 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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected 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
<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
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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获得所有cookieCookie[] cookieArr = req.getCookies();for (Cookie cookie : cookieArr) {if("fpl".equals(cookie.getName())){//修改fpl的失效时间cookie.setMaxAge(0);resp.addCookie(cookie);}}}
}
web.xml
<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>