JSP與Servlet之間設定Cookie可能導致Cookie無法讀取的解決辦法

來源:互聯網
上載者:User

在jsp中設定了Cookie,可在Servlet中無法讀取出相應的Cookie,搜了好多,最終是通過參看Cookie的API文檔解決的。其實解決方案很簡單,只要設定一下Cookie的path就可以了。另外,如果建立的Cookie不設定maxage的話,關閉瀏覽器之後該Cookie就失效了。

解決了此問題後,順便看了下Cookie的原理及相關的Java API文檔,整理如下: 

what is Cookie?

Cookie 是在http和https協議中,用來實現會話管理(session management),在使用者終端(比如瀏覽器)上儲存的一小段資料。一般用來儲存使用者的習慣(user preferences), 安全性要求較低的自動登入資訊, 以及收集使用者的資訊。它一般儲存在用戶端使用者瀏覽器的指定的目錄下,因此,盡量不要使用Cookie儲存敏感的資料,而且Cookie的資料最好加密。

每個Cookie都有個名字和對應的值。Cookie有幾個可選的屬性,比如注釋comment,路徑path,網域名稱domain,以及最大存留時間maxage,和版本version。由於現在的瀏覽器對這些屬性的支援存在bug,因此為了最大的互操作行,盡量不要太依靠這些屬性。

Cookies是在伺服器端被指定的,通過在HTTP響應的header中添加欄位來實現。在Java Servlet中,是通過response.addCookie方法來一次添加一個Cookie的。使用者瀏覽器一般被要求對於每個主機支援20個Cookie,每個Cookie最少支援4KB。使用大量的Cookie一般是不鼓勵的。當瀏覽器發出請求時,這些Cookie會被加在HTTP請求header中發到伺服器端。在伺服器端,可以通過request.getCookies來獲得本次請求中的所有的Cookies。位於不同path下可以存在相同名字的Cookie,在特定path下的Cookie只能被該path以及子目錄下的讀取。

1 publicvoid setDomain(String pattern);

Specifies the domain within which this cookie should be presented.
The form of the domain name is specified by RFC 2109. A domain name begins with a dot (.foo.com) and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone (for example, www.foo.com, but not a.b.foo.com). By default, cookies are only returned to the server that sent them.

1 publicvoid setMaxAge(int expiry);

Sets the maximum age of the cookie in seconds.
A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age. A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.

1 publicvoid setPath(String uri);

 Specifies a path for the cookie to which the client should return the cookie.
The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories. A cookie's path must include the servlet that set the cookie, for example, /catalog, which makes the cookie visible to all directories on the server under /catalog. Consult RFC 2109 (available on the Internet) for more information on setting path names for cookies. 

使用方法:

1、添加Cookie:

1 //建立一個Cookie,並設定名字和值。
2  cookie =new Cookie("cookiename","cookievalue");
3  //設定Cookie的有效期間為2天,如果不設定此有效期間,則使用者關閉瀏覽器後此Cookie失效。
4  cookie.setMaxAge(60*60*24*2);
5 //設定Cookie的有效路徑,“/”即該應用下都可以訪問該Cookie;
6 //如果不設定路徑,那麼只有設定該Cookie路徑及其子路徑可以訪問.
7 cookie.setPath("/");
8 //將Cookie添加到HTTP響應中去
9 response.addCookie(cookie);

2、獲得Cookie:

1 //擷取本次請求中的所有Cookies
2 Cookie[] cookies = request.getCookies();
3 if(cookies!=null){
4 for(Cookie cookie : cookies){
5 cookie.getName();
6 cookie.getValue();
7 }
8 }

注意:
1 Cookie的名字和值中盡量不要使用特殊字元。關於Cookie的名字,RFC2109中說name只能包含字母或者數字,不能包含逗號、分號、空格及貨幣符號。而在Cookie的value中建議不用使用逗號,分號,空白字元等。
2 如果jsp和Servlet之間設定和讀取Cookie時出現問題的話,一般是因為path的問題,此時通過調用Cookie.setPath來設定Cookie的path。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.