请选择 进入手机版 | 继续访问电脑版

湖南新梦想

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 86|回复: 0

Cookie

[复制链接]

15

主题

16

帖子

148

积分

注册会员

Rank: 2

积分
148
发表于 2023-2-8 23:39:21 | 显示全部楼层 |阅读模式
Cookie
cookie是保存在客户端的一个作用域。
与Session来说,它相对不安全。
只能存储字符串String类型的数据
通常用于存储用户的一些浏览足迹
因为不安全,如果是个人电脑,才存储用户的登录信息
也可以设置过期时间,默认是永不过期。

Cookie的数据会随每一次请求发送到服务器,每个网站都有自己的Cookie
读取Cookie//如果有Cookie,那么取Cookie数据
Cookie[] cookies = request.getCookies();
String name = "";
String pwd = "";
for (Cookie cookie : cookies) {
    if (cookie.getName().equals("userName")) {
        name = cookie.getValue();
    } else if (cookie.getName().equals("pwd")) {
        pwd = cookie.getValue();
    }
}
存入Cookieif (request.getParameter("isSave") != null) {
    //创建Cookie对象
    Cookie cookie1 = new Cookie("userName", username);
    Cookie cookie2 = new Cookie("pwd", pwd);
    //设置保存时间七天
    cookie1.setMaxAge(60 * 60 * 24 * 7);
    cookie2.setMaxAge(60 * 60 * 24 * 7);
    //向客户端写出
    response.addCookie(cookie1);
    response.addCookie(cookie2);
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|湖南新梦想 ( 湘ICP备18019834号-2 )

GMT+8, 2023-3-24 17:44 , Processed in 0.039373 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表