【Cookie】Jsp内置对象cookie小结          返回主页

Jsp内置对象Cookie作为持久化连接的方式值得写篇文章总结一下。

代码

创建Cookie

// new一个Cookie对象,键值对为参数  
Cookie cookie = new Cookie("key", "cookie的value值");  
     如果cookie的值中含有中文时,需要对cookie进行编码,不然会产生乱码,使用
           URLEncoder.encode("cookie的value值","utf-8");

// 设置Cookie最大生存时间,以秒为单位,负数的话为浏览器进程,关闭浏览器Cookie消失  
cookie.setMaxAge(*24*60*60);  // 一天  
// 将Cookie添加到Response中,使之生效  
response.addCookie(cookie);  //addCookie后,如果已经存在相同名字的cookie,则最新的覆盖旧的cookie
       注意:在Struts中可以使用 ServletActionContext.getResponse()获得respone对象

读取cookie

读取Cookie只能从request中获取全部Cookie,然后循环迭代。
在Struts中可以使用 ServletActionContext.getRequest()获得request对象
// 从request中获取Cookie,拿到的是一个Cookie数组  
Cookie[] cookies = request.getCookies();  
// 然后迭代之  
if (cookies != null && cookies.length > 0) { //如果没有设置过Cookie会返回null  
    for (Cookie cookie : cookies) {...}  
}  

删除cookie

删除Cookie的话,只需要将Cookie的生存期设为0即可
Cookie[] cookies = request.getCookies();  
if (cookies != null && cookies.length > 0) {  
    for (Cookie cookie : cookies) {  
        String name = cookie.getName();  
        // 找到需要删除的Cookie  
        if (name.compareTo("target-key") == 0) {  
            // 设置生存期为0  
            cookie.setMaxAge(0);  
            // 设回Response中生效  
            response.addCookie(cookie);  
        }  
    }  
}  

相关代码

解释

  1. new一个Cookie对象
  2. 设置最大生存时间
  3. response添加cookie String username = request.getParameter("username"); String password = request.getParameter("password");

    Cookie nameCookie = new Cookie("username", username); Cookie passCookie = new Cookie("password", password);

    nameCookie.setMaxAge(5 * 60); passCookie.setMaxAge(5 * 60);

    response.addCookie(nameCookie); response.addCookie(passCookie);

API

Cookie常见方法
 Object clone() 
          Overrides the standard java.lang.Object.clone method to return a copy of this cookie. 
 String getComment() 
          Returns the comment describing the purpose of this cookie, or null if the cookie has no comment. 
 String getDomain() 
          Returns the domain name set for this cookie. 
 int getMaxAge() 
          Returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown. 
 String getName() 
          Returns the name of the cookie. 
 String getPath() 
          Returns the path on the server to which the browser returns this cookie. 
 boolean getSecure() 
          Returns true if the browser is sending cookies only over a secure protocol, or false if the browser can send cookies using any protocol. 
 String getValue() 
          Returns the value of the cookie. 
 int getVersion() 
          Returns the version of the protocol this cookie complies with. 
 void setComment(String purpose) 
          Specifies a comment that describes a cookie's purpose. 
 void setDomain(String pattern) 
          Specifies the domain within which this cookie should be presented. 
 void setMaxAge(int expiry) 
          Sets the maximum age of the cookie in seconds. 
 void setPath(String uri) 
          Specifies a path for the cookie to which the client should return the cookie. 
 void setSecure(boolean flag) 
          Indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL. 
 void setValue(String newValue) 
          Assigns a new value to a cookie after the cookie is created. 
 void setVersion(int v) 
          Sets the version of the cookie protocol this cookie complies with.