cookie和session區別


首先明白一個概念,會話技術。會話:一次會話中包含多次請求和響應。

*一次會話:瀏覽器第一次給服務器資源發送請求,會話建立,直到有一方斷開為止
功能:在一次會話的范圍內的多次請求間,共享數據

 

Cookie Session 即為客戶端和服務器端對應的會話技術

不同點如下

cookie本質保存在客戶機中的簡單的文本文件,  保存了該客戶機訪問這個Web 文檔時的信息

Session是在服務端使用的一種數據結構,是一個抽象概念,用於記錄客戶端狀態,而cookie是實際存在的文件

cookie數據保存在客戶端,session數據保存在服務器端

session 實現依賴於cookie實現,想要在兩個session進行通信,需要一個類似通行證進行溝通,這就需要cookie實現,在cookie定義sessionid,在http通信服務端客戶端就可知道session對話

session對照身份使用表結構對照通信的id進行通信。

 Session 實現用哈希表(key-value鍵值對) 而Cookie 使用http表頭。

session對於數據大小比cookie限制要少,cookie翻譯為小甜點,看名字就知道不是大數據量存儲。瀏覽器對於單個cookie 的大小有限制(4kb) 以及 對同一個域名下的總cookie數量也有限制(20個)

session(主菜)要比cookie安全性要高

 

//session與cookie關聯案例
Cookie ck = new Cookie("SESSIONID",session.getId());
ck.setMaxAge(60*60);//設置生命周期
response.addCookie(ck);

  

 

以下為源碼

 

 

Cookie源碼

package javax.servlet.http;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public class Cookie implements Cloneable, Serializable {
    private static final long serialVersionUID = -6454587001725327448L;
    private static final String TSPECIALS;
    private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";
    private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");
    private String name;//Cookie的名稱
    private String value;//Cookie的值
    private String comment;//說明
    private String domain;//cookie域名,對於跨網站對話有關
    private int maxAge = -1;//cookie的壽命,單位秒,正數為壽命時間,0代表立即關閉,負數代表臨時開啟,關閉瀏覽器即失效

  private boolean secure;//安全協議傳輸是否 private int version = 0;//版本

  private boolean isHttpOnly = false; public Cookie(String name, String value) { if (name != null && name.length() != 0) { if (this.isToken(name) && !name.equalsIgnoreCase("Comment") && !name.equalsIgnoreCase("Discard") && !name.equalsIgnoreCase("Domain") && !name.equalsIgnoreCase("Expires") && !name.equalsIgnoreCase("Max-Age") && !name.equalsIgnoreCase("Path") && !name.equalsIgnoreCase("Secure") && !name.equalsIgnoreCase("Version") && !name.startsWith("$")) { this.name = name; this.value = value; } else { String errMsg = lStrings.getString("err.cookie_name_is_token"); Object[] errArgs = new Object[]{name}; errMsg = MessageFormat.format(errMsg, errArgs); throw new IllegalArgumentException(errMsg); } } else { throw new IllegalArgumentException(lStrings.getString("err.cookie_name_blank")); } } public void setComment(String purpose) { this.comment = purpose; } public String getComment() { return this.comment; } public void setDomain(String domain) { this.domain = domain.toLowerCase(Locale.ENGLISH); } public String getDomain() { return this.domain; } public void setMaxAge(int expiry) { this.maxAge = expiry; } public int getMaxAge() { return this.maxAge; } public void setPath(String uri) {//設置cookie的獲取范圍,共享可以改成“/” this.path = uri; } public String getPath() { return this.path; } public void setSecure(boolean flag) { this.secure = flag; } public boolean getSecure() { return this.secure; } public String getName() { return this.name; } public void setValue(String newValue) { this.value = newValue; } public String getValue() { return this.value; } public int getVersion() { return this.version; } public void setVersion(int v) { this.version = v; } private boolean isToken(String value) { int len = value.length(); for(int i = 0; i < len; ++i) { char c = value.charAt(i); if (c < ' ' || c >= 127 || TSPECIALS.indexOf(c) != -1) { return false; } } return true; } public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException var2) { throw new RuntimeException(var2.getMessage()); } } public void setHttpOnly(boolean isHttpOnly) { this.isHttpOnly = isHttpOnly; } public boolean isHttpOnly() { return this.isHttpOnly; } static { if (Boolean.valueOf(System.getProperty("org.glassfish.web.rfc2109_cookie_names_enforced", "true"))) { TSPECIALS = "/()<>@,;:\\\"[]?={} \t"; } else { TSPECIALS = ",; "; } } }

 

//HttpSession源碼 太懶了寫注釋 名字就是這個意思 有空補充

package javax.servlet.http;

import java.util.Enumeration;
import javax.servlet.ServletContext;

public interface HttpSession {
    long getCreationTime();

    String getId();

    long getLastAccessedTime();

    ServletContext getServletContext();

    void setMaxInactiveInterval(int var1);

    int getMaxInactiveInterval();

    /** @deprecated */
    @Deprecated
    HttpSessionContext getSessionContext();

    Object getAttribute(String var1);

    /** @deprecated */
    @Deprecated
    Object getValue(String var1);

    Enumeration<String> getAttributeNames();

    /** @deprecated */
    @Deprecated
    String[] getValueNames();

    void setAttribute(String var1, Object var2);

    /** @deprecated */
    @Deprecated
    void putValue(String var1, Object var2);

    void removeAttribute(String var1);

    /** @deprecated */
    @Deprecated
    void removeValue(String var1);

    void invalidate();

    boolean isNew();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM