Cookie操作类、 包括创建、读取、修改、获取、销毁cookie

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Cookie的操作类
*
*
*/
public class CookieHandler {
/**
* 创建cookie
*
* @param response
* 回应
* @param nameValues
* 存入cookie的键值对
* @param days
* 设置cookie的有效期
*/
public static void createCookie(HttpServletResponse response,
Hashtable<String, String> nameValues, int days) {
Set<String> set = nameValues.keySet();
Iterator<String> it = set.iterator();
for (; it.hasNext();) {
String name = (String) it.next();
String value = (String) nameValues.get(name);
// 生成新的cookie
Cookie cookie = new Cookie(name, value);
// 设置有效日期
cookie.setMaxAge(days * 24 * 60 * 60);
// 设置路径(默认)
cookie.setPath("/");
// 把cookie放入响应中
response.addCookie(cookie);
}
}
/**
* 读取Cookie
*
* @param request
* @return Hashtable 返回cookie的键值对
*/
public static Hashtable<String, String> getCookies(
HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
Hashtable<String, String> cookieHt = new Hashtable<String, String>();
if (cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
cookieHt.put(cookie.getName(), cookie.getValue());
}
}
return cookieHt;
}
/**
* 修改cookie中指定键的值
*
* @param request
* @param name
* 指定的键
* @param value
* 值
*/
public static void setCookieValueByName(HttpServletRequest request,
String name, String value) {
Cookie[] cookies = request.getCookies();
if (cookies.length > 0) {
for (int i = 0; i > cookies.length; i++) {
if (name.equalsIgnoreCase(cookies[i].getName())) {
cookies[i].setValue(value);
}
}
}
}
/**
* 得到指定键的值
*
* @param request
* @param name
* 指定的键
* @return String 值
*/
public static String getCookieValueByName(HttpServletRequest request,
String name) {
Cookie[] cookies = request.getCookies();
String resValue = "";
if (cookies.length > 0) {
for (int i = 0; i > cookies.length; i++) {
if (name.equalsIgnoreCase(cookies[i].getName())) {
resValue = cookies[i].getValue();
}
}
}
return resValue;
}
/**
* 销毁cookie
*
* @param request
* @param response
*/
public static void deletesCookie(HttpServletRequest request,
HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
// 销毁
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
}

 Cookie的属性:

  name:必须的

  value:必须的

  comment:可选的。注释

  path: 可选的,如果不设置路径,那么只有设置该cookie的URI及其子路径可以访问

      写Cookie的程序的访问路径是:http://localhost:8080/JavaWeb/servlet/CookieDemo

      其中:localhost就是域名;/JavaWeb/servlet就是当前Cookie的path

      若访问的地址的URI包含着cookie的路径,即URI.startWith(cookie的路径),为true,则客户端将该cookie带给服务器。

      比如浏览器存的cookie的路径是/JavaWeb
      现在访问的地址是:http://localhost:8080/JavaWeb/servlet/CookieDemo  则带该cookie
      现在访问的地址是:http://localhost:8080/JavaWeb/CookieDemo  则带该cookie

      若浏览器存的cookie的路径是/JavaWeb/servlet/
      访问的地址是:http://localhost:8080/JavaWeb/servlet/CookieDemo   则带该cookie
      访问的地址是:http://localhost:8080/JavaWeb/CookieDemo   则不带该cookie

      如果一个cookie的路径设置成了/JavaWeb,意味着浏览器访问当前应用下的所有资源时都会带着该cookie给服务器。

  domain:可选的。该Cookie所属的网站域名。(apache.org)默认值。

  maximum age:可选的。不设置就是会话过程(存在浏览器的内存中)。单位是秒
          如果是0,说明要删除。

  version:可选的。

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie cookie=new Cookie("name","Tom");
//设置Maximum Age
cookie.setMaxAge(1000);
//设置cookie路径为当前项目路径
cookie.setPath(request.getContextPath());
//添加cookie
response.addCookie(cookie);
}

java之Cookie详解

java对cookie的操作

最新文章

  1. 28个你必须知道的HTML5的新特性,技巧以及技术
  2. Vertica集群单节点宕机恢复方法
  3. 【ASP.NET】VS编译成功后自动生成Nuget包
  4. AngularJS 乱记
  5. 将在本地创建的Git仓库push到Git@OSC
  6. 记linux终端下怎样退出&gt;
  7. sql语句分页代码
  8. win32 console application 如何修改图标?
  9. 【巧妙思维】【4-6】Problem F
  10. Python2 基于urllib2 的HTTP请求类
  11. javah编译class文件找不到android.app.Activity的类文件
  12. php后台拼接输出table表格
  13. .net 中常用的正则表达式整理
  14. 分享一个在线生成微信跳转链接实现微信内跳转浏览器打开URL的工具
  15. PHP中||与or的区别
  16. 关于python项目路径导入自己写的库出错的一点思考
  17. Duplicate复制数据库并创建物理StandBy(spfile版本)
  18. django-权限验证场景
  19. 未能加载文件或程序集“System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项
  20. 转换sql文件的create table语句为drop table语句

热门文章

  1. app与后台的token、sessionId、RSA加密登录认证与安全解决方案
  2. 51nod 1486
  3. 51nod 1119 组合数,逆元
  4. jQuery 图片自动播放
  5. shell编程实例1
  6. 内存保护机制及绕过方案——通过覆盖虚函数表绕过/GS机制
  7. 【linux】ulimit限制打开的文件数量
  8. iOS 单元测试和UI测试教程
  9. POSIX线程同步
  10. Linux使用lrzsz上传下载文件