{"id":1014,"date":"2015-03-27T16:54:39","date_gmt":"2015-03-27T08:54:39","guid":{"rendered":"http:\/\/www.daliane.com\/?p=1014"},"modified":"2015-03-27T16:54:39","modified_gmt":"2015-03-27T08:54:39","slug":"js_cookie_ying_yong","status":"publish","type":"post","link":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/js_cookie_ying_yong\/","title":{"rendered":"js cookie\u5e94\u7528"},"content":{"rendered":"<p>&lt;!DOCTYPE HTML PUBLIC &#8220;-\/\/W3C\/\/DTD HTML 4.01\/\/EN&#8221; &#8220;http:\/\/www.w3.org\/TR\/html4\/strict.dtd&#8221;&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=GB2312&#8243; \/&gt;<br \/>\n&lt;title&gt;cookie\u5904\u7406\u51fd\u6570\u7ec3\u4e60(\u4e3a\u6211\u6240\u5199\uff0c\u975e\u6211\u6240\u60f3:\u6539\u5584\u9762\u5411\u5bf9\u8c61)&lt;\/title&gt;<br \/>\n&lt;script language=&#8221;JavaS<wbr><\/wbr>cript&#8221; type=&#8221;text\/javas<wbr><\/wbr>cript&#8221;&gt;<br \/>\nfunction addCookie(objName,objValue,objHours){\/\/\u6dfb\u52a0cookie<br \/>\nvar str = objName + &#8220;=&#8221; + escape(objValue);<br \/>\nif(objHours &gt; 0){\/\/\u4e3a0\u65f6\u4e0d\u8bbe\u5b9a\u8fc7\u671f\u65f6\u95f4\uff0c\u6d4f\u89c8\u5668\u5173\u95ed\u65f6cookie\u81ea\u52a8\u6d88\u5931<br \/>\nvar date = new Date();<br \/>\nvar ms = objHours*3600*1000;<br \/>\ndate.setTime(date.getTime() + ms);<br \/>\nstr += &#8220;; expires=&#8221; + date.toGMTString();<br \/>\n}<br \/>\ndocument.cookie = str;<br \/>\nalert(&#8220;\u6dfb\u52a0cookie\u6210\u529f&#8221;);<br \/>\n}<\/p>\n<p>function getCookie(objName){\/\/\u83b7\u53d6\u6307\u5b9a\u540d\u79f0\u7684cookie\u7684\u503c<br \/>\nvar arrStr = document.cookie.split(&#8220;; &#8220;);<br \/>\nfor(var i = 0;i &lt; arrStr.length;i ++){<br \/>\nvar temp = arrStr[i].split(&#8220;=&#8221;);<br \/>\nif(temp[0] == objName) return unescape(temp[1]);<br \/>\n}<br \/>\n}<\/p>\n<p>function delCookie(name){\/\/\u4e3a\u4e86\u5220\u9664\u6307\u5b9a\u540d\u79f0\u7684cookie\uff0c\u53ef\u4ee5\u5c06\u5176\u8fc7\u671f\u65f6\u95f4\u8bbe\u5b9a\u4e3a\u4e00\u4e2a\u8fc7\u53bb\u7684\u65f6\u95f4<br \/>\nvar date = new Date();<br \/>\ndate.setTime(date.getTime() &#8211; 10000);<br \/>\ndocument.cookie = name + &#8220;=a; expires=&#8221; + date.toGMTString();<br \/>\n}<br \/>\n\/\/\u8bfb\u53d6\u51fa\u6765\u6240\u6709\u7684cookie\u5b57\u7b57\u4e32\u4e86<br \/>\nfunction allCookie(){\/\/\u8bfb\u53d6\u6240\u6709\u4fdd\u5b58\u7684cookie\u5b57\u7b26\u4e32<br \/>\nvar str = document.cookie;<br \/>\nif(str == &#8220;&#8221;){<br \/>\nstr = &#8220;\u6ca1\u6709\u4fdd\u5b58\u4efb\u4f55cookie&#8221;;<br \/>\n}<br \/>\nalert(str);<br \/>\n}<\/p>\n<p>function $(m,n){<br \/>\nreturn document.forms[m].elements[n].value;<br \/>\n}<\/p>\n<p>function add_(){<br \/>\nvar cookie_name = $(&#8220;myform&#8221;,&#8221;cookie_name&#8221;);<br \/>\nvar cookie_value = $(&#8220;myform&#8221;,&#8221;cookie_value&#8221;);<br \/>\nvar cookie_expireHours = $(&#8220;myform&#8221;,&#8221;cookie_expiresHours&#8221;);<br \/>\naddCookie(cookie_name,cookie_value,cookie_expireHours);<br \/>\n}<\/p>\n<p>function get_(){<br \/>\nvar cookie_name = $(&#8220;myform&#8221;,&#8221;cookie_name&#8221;);<br \/>\nvar cookie_value = getCookie(cookie_name);<br \/>\nalert(cookie_value);<br \/>\n}<\/p>\n<p>function del_(){<br \/>\nvar cookie_name = $(&#8220;myform&#8221;,&#8221;cookie_name&#8221;);<br \/>\ndelCookie(cookie_name);<br \/>\nalert(&#8220;\u5220\u9664\u6210\u529f&#8221;);<br \/>\n}<br \/>\n&lt;\/script&gt;<\/p>\n<p>\/\/\u6dfb\u52a0cookie<br \/>\nfunction addCookie(name,value,expires,path,domain){<br \/>\nvar str=name+&#8221;=&#8221;+escape(value);<br \/>\nif(expires!=&#8221;&#8221;){<br \/>\nvar date=new Date();<br \/>\ndate.setTime(date.getTime()+expires*24*3600*1000);\/\/expires\u5355\u4f4d\u4e3a\u5929<br \/>\nstr+=&#8221;;expires=&#8221;+date.toGMTString();<br \/>\n}<br \/>\nif(path!=&#8221;&#8221;){<br \/>\nstr+=&#8221;;path=&#8221;+path;\/\/\u6307\u5b9a\u53ef\u8bbf\u95eecookie\u7684\u76ee\u5f55<br \/>\n}<br \/>\nif(domain!=&#8221;&#8221;){<br \/>\nstr+=&#8221;;domain=&#8221;+domain;\/\/\u6307\u5b9a\u53ef\u8bbf\u95eecookie\u7684\u57df<br \/>\n}<br \/>\ndocument.cookie=str;<br \/>\n}<br \/>\n\/\/\u53d6\u5f97cookie<br \/>\nfunction getCookie(name){<br \/>\nvar str=document.cookie.split(&#8220;;&#8221;)<br \/>\nfor(var i=0;i&lt;str.length;i++){<br \/>\nvar str2=str[i].split(\u201d=\u201d);<br \/>\nif(str2[0]==name)return unescape(str2[1]);<br \/>\n}<br \/>\n}<br \/>\n\/\/\u5220\u9664cookie<br \/>\nfunction delCookie(name){<br \/>\nvar date=new Date();<br \/>\ndate.setTime(date.getTime()-10000);<br \/>\ndocument.cookie=name+\u201d=n;expire=\u201d+date.toGMTString();<\/p>\n<p>&nbsp;<\/p>\n<p>[\u4e0b\u9762\u8bb2\u7684\u7ecf\u4e2a\u4eba\u89c9\u5f97\u6bd4\u8f83\u597d\u54e6\uff01]<br \/>\n\u6211\u4eec\u5f53\u7136\u8fd8\u5f97\u4ecb\u7ecdcookie\u7684\u56db\u4e2a\u5c5e\u6027\u3002\u8fd9\u4e9b\u5c5e\u6027\u7528\u4e0b\u9762\u7684\u683c\u5f0f\u52a0\u5230\u5b57\u7b26\u4e32\u503c\u540e\u9762\uff1a<\/p>\n<p>name=&lt;value&gt;[; expires=&lt;date&gt;][; domain=&lt;domain&gt;][; path=&lt;path&gt;][; secure]<\/p>\n<p>\u540d\u79f0=&lt;\u503c&gt;[; expires=&lt;\u65e5\u671f&gt;][; domain=&lt;\u57df&gt;][; path=&lt;\u8def\u5f84&gt;][; \u5b89\u5168]<\/p>\n<p>&lt;value&gt;, &lt;date&gt;, &lt;domain&gt; \u548c &lt;path&gt; \u5e94\u5f53\u7528\u5bf9\u5e94\u7684\u503c\u66ff\u6362\u3002&lt;date&gt; \u5e94\u5f53\u4f7f\u7528GMT\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Javas<wbr><\/wbr>cript\u811a\u672c\u8bed\u8a00\u7684\u65e5\u671f\u7c7bDate\u7684.toGMTString() \u65b9\u6cd5\u5f97\u5230\u8fd9\u4e00GMT\u683c\u5f0f\u7684\u65e5\u671f\u503c\u3002\u65b9\u62ec\u53f7\u4ee3\u8868\u8fd9\u9879\u662f\u53ef\u9009\u7684\u3002\u6bd4\u5982\u5728 [; secure]\u4e24\u8fb9\u7684\u65b9\u62ec\u53f7\u4ee3\u8868\u8981\u60f3\u628acookie\u8bbe\u7f6e\u6210\u5b89\u5168\u7684\uff0c\u5c31\u9700\u8981\u628a&#8221;; secure&#8221; \u52a0\u5230cookie\u5b57\u7b26\u4e32\u503c\u7684\u540e\u9762\u3002\u5982\u679c&#8221;; secure&#8221; \u6ca1\u6709\u52a0\u5230cookie\u5b57\u7b26\u4e32\u540e\u9762\uff0c\u90a3\u4e48\u8fd9\u4e2acookie\u5c31\u662f\u4e0d\u5b89\u5168\u7684\u3002\u4e0d\u8981\u628a\u5c16\u62ec\u53f7&lt;&gt; \u548c\u65b9\u62ec\u53f7[] \u52a0\u5230cookie\u91cc\uff08\u9664\u975e\u5b83\u4eec\u662f\u67d0\u4e9b\u503c\u7684\u5185\u5bb9\uff09\u3002\u8bbe\u7f6e\u5c5e\u6027\u65f6\uff0c\u4e0d\u9650\u5c5e\u6027\uff0c\u53ef\u4ee5\u7528\u4efb\u4f55\u987a\u5e8f\u8bbe\u7f6e\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0ccookie &#8220;username&#8221; \u88ab\u8bbe\u7f6e\u6210\u572815\u5206\u949f\u4e4b\u540e\u8fc7\u671f\uff0c\u53ef\u4ee5\u88ab\u670d\u52a1\u5668\u4e0a\u7684\u6240\u6709\u76ee\u5f55\u8bbf\u95ee\uff0c\u53ef\u4ee5\u88ab&#8221;mydomain.com&#8221;\u57df\u91cc\u7684\u6240\u6709\u670d\u52a1\u5668\u8bbf\u95ee\uff0c\u5b89\u5168\u72b6\u6001\u4e3a\u5b89\u5168\u3002<br \/>\n\/\/ Date() \u7684\u6784\u9020\u5668\u8bbe\u7f6e\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d<br \/>\n\/\/ .getTime() \u65b9\u6cd5\u8fd4\u56de\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u6beb\u79d2<br \/>\n\/\/ \u6240\u4ee5\u8981\u8bbe\u7f6e15\u5206\u949f\u5230\u671f\uff0c\u8981\u752860000\u6beb\u79d2\u4e5815\u5206\u949f<br \/>\nvar expiration = new Date((new Date()).getTime() + 15 * 60000);<br \/>\ndocument.cookie = &#8220;username=&#8221; + escape(form.username.value)+ &#8220;; expires =&#8221;<br \/>\n+ expiration.toGMTString() + &#8220;; path=&#8221; + &#8220;\/&#8221; + &#8220;; _<br \/>\ndomain=&#8221; + &#8220;mydomain.com&#8221; + &#8220;; secure&#8221;;<\/p>\n<p>\/\/ \u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u6765\u8bfb\u53d6\u7279\u5b9a\u7684cookie\u503c\u3002\uff3b\u5f97\u5230\u6307\u5b9a\u540d\u5b57\u7684cookie\u5bf9\u8c61\u54e6\uff01\uff3d<br \/>\nfunction getCookie(cookie_name)<br \/>\n{<br \/>\nvar allcookies = document.cookie;<br \/>\nvar cookie_pos = allcookies.indexOf(cookie_name);<\/p>\n<p>\/\/ \u5982\u679c\u627e\u5230\u4e86\u7d22\u5f15\uff0c\u5c31\u4ee3\u8868cookie\u5b58\u5728\uff0c<br \/>\n\/\/ \u53cd\u4e4b\uff0c\u5c31\u8bf4\u660e\u4e0d\u5b58\u5728\u3002<br \/>\nif (cookie_pos != -1)<br \/>\n{<br \/>\n\/\/ \u628acookie_pos\u653e\u5728\u503c\u7684\u5f00\u59cb\uff0c\u53ea\u8981\u7ed9\u503c\u52a01\u5373\u53ef\u3002<br \/>\ncookie_pos += cookie_name.length + 1;<br \/>\nvar cookie_end = allcookies.indexOf(&#8220;;&#8221;, cookie_pos);<\/p>\n<p>if (cookie_end == -1)<br \/>\n{<br \/>\ncookie_end = allcookies.length;<br \/>\n}<\/p>\n<p>var value = unescape(allcookies.substring(cookie_pos, cookie_end));<br \/>\n}<\/p>\n<p>return value;<br \/>\n}<\/p>\n<p>\/\/ \u8c03\u7528\u51fd\u6570<br \/>\nvar cookie_val = getCookie(&#8220;username&#8221;);<\/p>\n<p>3.\u4e3a\u4ec0\u4e48\u6211\u8bbe\u7f6e\u4e86cookie\u7684\u8fc7\u671f\u65f6\u95f4\u5982\u679c\u4e3a\u5173\u95ed\u7684\u65f6\u5019\u5c31\u81ea\u52a8\u6e05\u7a7a\u7684\u8bdd\u600e\u4e48\u6ca1\u7528\u5462\uff1f<br \/>\n\u6765\u7814\u7a76\u4e00\u4e0bJSP\u64cd\u7eb5cookie?<br \/>\nCookie\u6982\u5ff5:<br \/>\nCookie\u7684\u683c\u5f0f\u5b9e\u9645\u4e0a\u662f\u4e00\u6bb5\u7eaf\u6587\u672c\u4fe1\u606f, \u7531\u670d\u52a1\u5668\u968f\u7740\u7f51\u9875\u4e00\u8d77\u53d1\u9001\u5230\u5ba2\u6237\u7aef, \u5e76\u4fdd\u5b58\u5728\u5ba2\u6237\u7aef\u786c\u76d8\u4e2d\u6307\u5b9a\u7684\u76ee\u5f55\u7684. \u5927\u5bb6\u90fd\u4f20\u8bf4Cookie\u4f1a\u9020\u6210\u4e25\u91cd\u7684\u5b89\u5168\u5a01\u80c1\u4ec0\u4e48\u7684, \u5176\u5b9e\u4e0d\u662f\u8fd9\u4e48\u56de\u4e8b\u60c5. \u670d\u52a1\u5668\u8bfb\u53d6Cookie\u7684\u65f6\u5019, \u53ea\u80fd\u591f\u8bfb\u53d6\u5230\u8fd9\u4e2a\u670d\u52a1\u5668\u76f8\u5173\u7684\u4fe1\u606f. \u800c\u4e14, \u6d4f\u89c8\u5668\u4e00\u822c\u53ea\u5141\u8bb8\u5b58\u653e300\u4e2aCookie, \u6bcf\u4e2a\u7ad9\u70b9\u6700\u591a\u5b58\u653e20\u4e2a, \u800c\u4e14, \u6bcf\u4e2aCookie\u7684\u5927\u5c0f\u73b0\u5728\u57284K, \u6839\u672c\u4e0d\u4f1a\u5360\u7528\u591a\u5c11\u7a7a\u95f4. \u5e76\u4e14, Cookie\u662f\u6709\u65f6\u6548\u6027\u8d28\u7684. \u4f8b\u5982, \u8bbe\u7f6e\u4e86Cookie\u7684\u5b58\u6d3b\u65f6\u95f4\u4e3a1\u5206\u949f, \u5219\u4e00\u5206\u949f\u540e\u8fd9\u4e2aCookie\u5c31\u4f1a\u88ab\u6d4f\u89c8\u5668\u5220\u9664<\/p>\n<p>Cookie\u7248\u672c:<br \/>\n\u76ee\u524d\u6709\u4e24\u4e2a\u7248\u672c:<br \/>\n\u7248\u672c0 : \u7531Netscape\u516c\u53f8\u5236\u5b9a\u7684\uff0c\u4e5f\u88ab\u51e0\u4e4e\u6240\u6709\u7684\u6d4f\u89c8\u5668\u652f\u6301. Java\u4e2d\u4e3a\u4e86\u4fdd\u6301\u517c\u5bb9\u6027, \u76ee\u524d\u53ea\u652f\u6301\u5230\u7248\u672c0, Cookie\u7684\u5185\u5bb9\u4e2d\u4e0d\u80fd\u7a7a\u683c\uff0c\u65b9\u62ec\u53f7\uff0c\u5706\u62ec\u53f7\uff0c\u7b49\u4e8e\u53f7\uff08=\uff09\uff0c\u9017\u53f7\uff0c\u53cc\u5f15\u53f7\uff0c\u659c\u6760\uff0c\u95ee\u53f7\uff0c@\u7b26\u53f7\uff0c\u5192\u53f7\uff0c\u5206\u53f7\u3002<br \/>\n\u7248\u672c1 : \u6839\u636eRFC 2109\u6587\u6863\u5236\u5b9a\u7684. \u653e\u5bbd\u4e86\u5f88\u591a\u9650\u5236. \u4e0a\u9762\u6240\u9650\u5236\u7684\u5b57\u7b26\u90fd\u53ef\u4ee5\u4f7f\u7528. \u4f46\u4e3a\u4e86\u4fdd\u6301\u517c\u5bb9\u6027, \u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26.<\/p>\n<p>JSP\u4e2d\u5bf9Cookie\u7684\u64cd\u4f5c: \u7c7b\u578b \u65b9\u6cd5\u540d \u65b9\u6cd5\u89e3\u91ca<br \/>\nString getComment() \u8fd4\u56decookie\u4e2d\u6ce8\u91ca,\u5982\u679c\u6ca1\u6709\u6ce8\u91ca\u7684\u8bdd\u5c06\u8fd4\u56de\u7a7a\u503c.<br \/>\nString getDomain() \u8fd4\u56decookie\u4e2dCookie\u9002\u7528\u7684\u57df\u540d. \u4f7f\u7528getDomain() \u65b9\u6cd5\u53ef\u4ee5\u6307\u793a\u6d4f\u89c8\u5668\u628aCookie\u8fd4\u56de\u7ed9\u540c \u4e00\u57df\u5185\u7684\u5176\u4ed6\u670d\u52a1\u5668,\u800c\u901a\u5e38 Cookie\u53ea\u8fd4\u56de\u7ed9\u4e0e\u53d1\u9001\u5b83\u7684\u670d\u52a1\u5668\u540d\u5b57\u5b8c\u5168\u76f8\u540c\u7684\u670d\u52a1\u5668\u3002\u6ce8\u610f\u57df\u540d\u5fc5\u987b\u4ee5\u70b9\u5f00\u59cb<br \/>\nint getMaxAge() \u8fd4\u56deCookie\u8fc7\u671f\u4e4b\u524d\u7684\u6700\u5927\u65f6\u95f4\uff0c\u4ee5\u79d2\u8ba1\u7b97\u3002<br \/>\nString getName() \u8fd4\u56deCookie\u7684\u540d\u5b57<br \/>\nString getPath() \u8fd4\u56deCookie\u9002\u7528\u7684\u8def\u5f84\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u8def\u5f84\uff0cCookie\u5c06\u8fd4\u56de\u7ed9\u5f53\u524d\u9875\u9762\u6240\u5728\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\u4e0b \u7684\u6240\u6709\u9875\u9762\u3002<br \/>\nboolean getSecure() \u5982\u679c\u6d4f\u89c8\u5668\u901a\u8fc7\u5b89\u5168\u534f\u8bae\u53d1\u9001cookies\u5c06\u8fd4\u56detrue\u503c\uff0c\u5982\u679c\u6d4f\u89c8\u5668\u4f7f\u7528\u6807\u51c6\u534f\u8bae\u5219\u8fd4\u56defalse\u503c\u3002<br \/>\nString getValue() \u8fd4\u56deCookie\u7684\u503c\u3002\u7b14\u8005\u4e5f\u5c06\u5728\u540e\u9762\u8be6\u7ec6\u4ecb\u7ecdgetValue\/setValue\u3002<br \/>\nint getVersion() \u8fd4\u56deCookie\u6240\u9075\u4ece\u7684\u534f\u8bae\u7248\u672c\u3002<br \/>\nvoid setComment(String purpose) \u8bbe\u7f6ecookie\u4e2d\u6ce8\u91ca<br \/>\nvoid setDomain(String pattern) \u8bbe\u7f6ecookie\u4e2dCookie\u9002\u7528\u7684\u57df\u540d<br \/>\nvoid setMaxAge(int expiry) \u4ee5\u79d2\u8ba1\u7b97\uff0c\u8bbe\u7f6eCookie\u8fc7\u671f\u65f6\u95f4\u3002<br \/>\nvoid setPath(String uri) \u6307\u5b9aCookie\u9002\u7528\u7684\u8def\u5f84\u3002<br \/>\nvoid setSecure(boolean flag) \u6307\u51fa\u6d4f\u89c8\u5668\u4f7f\u7528\u7684\u5b89\u5168\u534f\u8bae\uff0c\u4f8b\u5982HTTPS\u6216SSL\u3002<br \/>\nvoid setValue(String newValue) cookie\u521b\u5efa\u540e\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684\u503c\u3002<br \/>\nvoid setVersion(int v) \u8bbe\u7f6eCookie\u6240\u9075\u4ece\u7684\u534f\u8bae\u7248\u672c<\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50<br \/>\n1. \u5199\u5165Cookie &#8212; writecookie.jsp<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n&lt;%@ page contentType=&#8221;text\/html; charset=ISO8859_1&#8243; %&gt;<br \/>\n&lt;%<br \/>\nCookie _cookie=new Cookie(&#8220;user_delfancom&#8221;, &#8220;delfan&#8221;);<br \/>\n_cookie.setMaxAge(30*60); \/\/ \u8bbe\u7f6eCookie\u7684\u5b58\u6d3b\u65f6\u95f4\u4e3a30\u5206\u949f<br \/>\nresponse.addCookie(_cookie); \/\/ \u5199\u5165\u5ba2\u6237\u7aef\u786c\u76d8<br \/>\nout.print(&#8220;\u5199Cookie\u5b8c\u6210&#8221;);<br \/>\n%&gt;<\/p>\n<p>2. \u8bfb\u53d6Cookie.jsp &#8212; readcookie.jsp<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n&lt;%<br \/>\nCookie cookies[]=request.getCookies(); \/\/ \u5c06\u9002\u7528\u76ee\u5f55\u4e0b\u6240\u6709Cookie\u8bfb\u5165\u5e76\u5b58\u5165cookies\u6570\u7ec4\u4e2d<br \/>\nCookie sCookie=null;<br \/>\nString sname=null;<br \/>\nString name=null;<br \/>\nif(cookies==null) \/\/ \u5982\u679c\u6ca1\u6709\u4efb\u4f55cookie<br \/>\nout.print(&#8220;none any cookie&#8221;);<br \/>\nelse<br \/>\n{<br \/>\nout.print(cookies.length + &#8220;&lt;br&gt;&#8221;);<br \/>\nfor(int i=0;i&lt;cookies.length; i++) \/\/ \u5faa\u73af\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684Cookie<br \/>\n{<br \/>\nsCookie=cookies[i];<br \/>\nsname=sCookie.getName();<br \/>\nname = sCookie.getValue();<br \/>\nout.println(sname + &#8220;-&gt;&#8221; + name + &#8220;&lt;br&gt;&#8221;);<br \/>\n}<br \/>\n}<br \/>\n%&gt;<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u4e24\u4e2a\u95ee\u9898:<br \/>\n1. Cookie\u6709\u4e2a\u9002\u7528\u8def\u5f84\u7684\u95ee\u9898, \u5c31\u662f\u8bf4\u5982\u679c writecookie.jsp\u548creadcookie.jsp\u8981\u653e\u5728\u540c\u610f\u76ee\u5f55\u4e0b, \u5982\u679c\u4e0d\u5728\u540c\u4e00\u76ee\u5f55\u4e0b, \u5219\u5199\u7684\u65f6\u5019\u9700\u8981\u8bbe\u7f6e\u8def\u5f84,\u4e3areadcookie.jsp\u6240\u5728\u7684\u8def\u5f84.<br \/>\n2. \u8bfb\u5165Cookie\u6570\u7ec4\u7684\u65f6\u5019\u9700\u8981\u5224\u65ad\u662f\u5426\u4e3a\u7a7a(null), \u7f51\u4e0a\u5f88\u591a\u4ee3\u7801\u90fd\u6ca1\u6709\u5199\u51fa\u8fd9\u4e00\u70b9.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;!DOCTYPE HTML PUBLIC &#8220;-\/\/W3C\/\/DTD HTML 4.01\/\/&#8230;<\/p>\n","protected":false},"author":2,"featured_media":228,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[38,60],"tags":[233],"_links":{"self":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/1014"}],"collection":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/comments?post=1014"}],"version-history":[{"count":1,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/1014\/revisions"}],"predecessor-version":[{"id":1015,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/1014\/revisions\/1015"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media\/228"}],"wp:attachment":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media?parent=1014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/categories?post=1014"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/tags?post=1014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}