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

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的操作

posted @ 2017-03-17 13:41  kouhanyao  阅读(1155)  评论(0编辑  收藏  举报