JavaWeb Cookie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
public class CookieDemo extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建cookie对象
        Cookie c = new Cookie("msg","hello");
        //2.发送cookie
        resp.addCookie(c);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }
}
 
public class CookieDemo2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //3.获取Cookie对象
        Cookie[] cookies = req.getCookies();
        //获取数据,遍历Cookie
        if (cookies != null){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println(name+value);
            }
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }
}
 
public class CookieDemo3 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建多cookie对象
        Cookie c1 = new Cookie("msg","hello");
        Cookie c2 = new Cookie("name","zhangsan");
        //2.发送多个cookie
        resp.addCookie(c1);
        resp.addCookie(c2);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }
}
 
public class CookieDemo4 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建cookie对象
        Cookie c1 = new Cookie("msg","setMaxAge");
        //2.设置Cookie的存活时间
        c1.setMaxAge(30);//将cookie持久化到硬盘30秒,30秒后会自动删除文件
        //3.发送cookie
        resp.addCookie(c1);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }
}
 
public class CookieDemo5 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建cookie对象
        Cookie c1 = new Cookie("msg","setMaxAge");
        //2.设置Cookie的存活时间
        c1.setMaxAge(30);//将cookie持久化到硬盘30秒,30秒后会自动删除文件
        //3.设置cookie的共享范围
        c1.setPath("/"); //根路径("/"),项目("/project")
        //4.不同的服务器之间共享问题
        c1.setDomain(".baidu.com"); //如果设置一级域名相同,那么多个服务器之间cookie可以共享
        //setDomain(".baidu.com"),那么tieba.baidu.com和news.baidu.com中cookie可以共享
        //3.发送cookie
        resp.addCookie(c1);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }
}
 
 
 
/**
 * 在服务器中的Servlet判断是否有一个名为lastTime的cookie
 * 1.有:不是第一次访问
 * -响应数据:欢迎回来,您上次访问时间为:***
 * -写回Cookie: lastTime=*****
 * 2.没有,是第一次访问
 * -响应数据:您好欢迎您首次访问
 * -写回Cookie:lastTime=****
 */
public class cookieTest extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应的的数据格式编码
        resp.setContentType("text/html;charset=utf-8");
        //1.获取所有Cookie判断名称是否为lastTime
        Cookie[] cookies = req.getCookies();
        boolean flag = false;
        //2.遍历Cookie数组
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                //3.获取cookie名称
                String name = cookie.getName();
                //4.判断名称是否是lastTime
                if ("lastTime".equals(name)) {
                    flag = true;
                    //5.有该cookie则不是第一次访问
                    //6.设置响应数据,设置cookie 的value
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    String str_date = sdf.format(date);
                    //URL编码去掉空格
                    str_date = URLEncoder.encode(str_date, "utf-8");
                    cookie.setValue(str_date);
                    //设置cookie存活期为一个月
                    cookie.setMaxAge(60 * 60 * 24 * 30);
                    //获取当前时间字符串后,重新发送cookie
                    String value = cookie.getValue();
                    //URL解码
                    value = URLDecoder.decode(value,"utf-8");
                    resp.getWriter().write("<h1>欢迎回来,您上次访问的时间为:" + value + "</h1>");
                    resp.addCookie(cookie);
                }
            }
        }
        if (cookies == null || cookies.length == 0 || !flag) {
            //如果没有cookie那么这是第一次访问
            //1.响应数据,设置cookie的value
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String str_date = sdf.format(date);
            //URL编码去掉空格
            str_date = URLEncoder.encode(str_date, "utf-8");
            //新建Cookie将k,v传入
            Cookie cookie = new Cookie("lastTime", str_date);
            //设置cookie存活时间为一个月
            cookie.setMaxAge(60 * 60 * 24 * 30);
            //获取字符串,重新发送cookie
            resp.addCookie(cookie);
            //获取当前时间字符串后,重新发送cookie
            String value = cookie.getValue();
            //URL解码
            value = URLDecoder.decode(value,"utf-8");
            resp.getWriter().write("<h1>您好,欢迎您首次访问</h1>");
        }
    }
}

  

posted @   MineLSG  阅读(85)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示