Home Servlet如何使用Cookie
Post
Cancel

Servlet如何使用Cookie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** 创建Cookie */
// 键值形式创建Cookie
Cookie cookie = new Cookie("temp", tempData);
// 设置Cookie的存活时间为秒
cookie.setMaxAge(60 * 60 * 24);
// 写入客户端硬盘
response.addCookie(cookie);

/** 调用Cookie */
// 将目录下所有Cookie读入并存入Cookies数组中
Cookie cookies[] = request.getCookies();
Cookie sCookie = null;
if (cookies != null) {
    // 循环列出所有可用的Cookie
    for(int i = 0; i < cookies.length; i++) {
        sCookie = cookies[i];
        String sname = sCookie.getName();
        String name = sCookie.getValue();
    }
}
This post is licensed under CC BY 4.0 by the author.