当前位置:网站首页>封装Cookie API
封装Cookie API
2022-07-30 05:40:00 【北木桥溪】
Cookie: 在客户端保存服务器数据,在客户端实现数据共享.
* cookie.setMaxAge(); cookie生命周期
* cookie.setMaxAge(0); 立即删除cookie
* cookie.setMaxAge(100); 设定100秒有效期 100秒之后自动删除
* cookie.setMaxAge(-1); 关闭会话后删除
设定path cookie的权限设定
* cookie.setPath(“/”) 一般条件下设定为/ 通用
* 权限:根目录及其子目录有效
* cookie.setPath(“/user”)
* 权限:/user目录下有效
设定Cookie资源共享
* cookie特点: 自己的域名下,只能看到自己的Cookie. 默认条件下不能共享的
* cookie.setDomain(“jt.com”); 只有在xxx.jt.com的域名中实现数据共享
package com.xxx.util;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//工具API 主要负责 新增cookie 删除cookie 根据key获取cookie 获取cookie的值
public class CookieUtil {
public static void addCookie(HttpServletResponse response,String name, String value, int maxAge, String path, String domain){
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(maxAge);
cookie.setPath(path);
cookie.setDomain(domain);
response.addCookie(cookie);
}
public static void delCookie(HttpServletResponse response,String name,String path, String domain){
addCookie(response, name, "", 0, path, domain);
}
public static Cookie getCookie(HttpServletRequest request,String name){
Cookie[] cookies = request.getCookies();
if(cookies !=null && cookies.length >0){
for (Cookie cookie : cookies){
if(name.equals(cookie.getName())){
return cookie;
}
}
}
return null;
}
public static String getCookieValue(HttpServletRequest request,String name){
Cookie cookie = getCookie(request, name);
return cookie==null?null:cookie.getValue();
}
}
边栏推荐
- 453.最小操作数使数组元素相等
- ‘kaggle视频游戏销售数据的可视化和分析‘项目实现
- 208.实现Trie树(字典树与哈希表实现)
- MySQL (2)
- MySQL 灵魂 16 问,你能撑到第几问?
- G Bus Count (Google Kickstart2014 Round D Problem B) (DAY 89)
- [Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
- Socket通信编程
- argparse —— 命令行选项、参数和子命令解析器
- VS2022中关于scanf函数报错解决方法
猜你喜欢

navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)

pytorch中的线性代数

650.只有两个键的键盘(动态规划)

Socket通信编程

Memories · The last system design in the university era

50道SQL练习题(刷完直接进大厂)

安装Nuxt.js时出现错误:TypeError:Cannot read property ‘eslint‘ of undefined

函数解剖——深挖printf()与scanf()

Detailed MySQL-Explain

208.实现Trie树(字典树与哈希表实现)
随机推荐
Qt在QTableWidget、View等表格中添加右击菜单
Falling ants (Peking University entrance exam questions)
“tensorflow.keras.preprocessing“ has no attribute “image_dataset_from_directory“
C语言(1)
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
240.搜索二维矩阵II
list(列表)和array(数组)的区别
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
爬虫数据是如何收集和整理的?
MySQL的存储过程
C语言指针(指针数组、数组指针、函数指针、传参、回调函数等)超详细
零基础C语言“函数”教程,有手就行
mysql time field is set to current time by default
Ranking of grades (Huazhong University of Science and Technology postgraduate examination questions) (DAY 87)
pytorch中的线性代数
ClickHouse data insert, update and delete operations SQL
C语言:快速排序三种方法(递归)
flask的笔记
Numpy 中 np.vstack() 和 np.hstack() 简单解析
每日练习------输出一个整数的二进制数、八进制数、十六进制数。