当前位置:网站首页>封装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();
}
}
边栏推荐
- MySQL 灵魂 16 问,你能撑到第几问?
- 【Pytorch】torch.manual_seed()、torch.cuda.manual_seed() 解释
- What is SOA (Service Oriented Architecture)?
- Difference between cookie and session
- 面试前需要巩固的算法知识点(自用,更新中)
- Qt通过QSttings类读取*.ini配置文件
- 【线性神经网络】线性回归 / 基础优化方法
- Frobenius norm(Frobenius 范数)
- mysql time field is set to current time by default
- Introduction to Oracle Patch System and Opatch Tool
猜你喜欢

mysql 时间字段默认设置为当前时间

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

‘kaggle视频游戏销售数据的可视化和分析‘项目实现

navicat新建数据库

mysql time field is set to current time by default

More fragrant open source projects than Ruoyi in 2022

optimizer.zero_grad()

flask-socketio实现的网页聊天室(二)

Qt通过QSttings类读取*.ini配置文件

Learn FPGA from the underlying structure (6) ---- Distributed RAM (DRAM, Distributed RAM)
随机推荐
CISP-PTE真题演示
Detailed MySQL-Explain
torch.optim.Adam()
MySql fuzzy query Daquan
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
C语言(入门篇一)
‘kaggle视频游戏销售数据的可视化和分析‘项目实现
G Bus Count (Google Kickstart2014 Round D Problem B) (DAY 89)
mysql time field is set to current time by default
Navicat connection MySQL error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
Prime numbers (Tsinghua University computer test questions) (DAY 86)
配环境 / 初步测试
留念 · 大学时代最后的系统设计图
Teach you to completely uninstall MySQL
三子棋游戏实现(c语言)
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
【C语言】三子棋(井字棋)的实现
Record Breaker (Google Kickstart2020 Round D Problem A)
SRA数据下载方法总结
I/O多路复用技术