涓枃灞炰簬Unicode瀛楃锛屽湪鍐呭瓨涓崰4涓瓧绗︼紝鑰岃嫳鏂囧睘浜嶢SCII瀛楃锛屽唴瀛樹腑鍙崰2涓瓧鑺?/strong>銆侰ookie涓娇鐢║nicode瀛楃鏃堕渶瑕佸Unicode瀛楃杩涜缂栫爜锛屽惁鍒欎細涔辩爜銆?/p>
鎻愮ず锛欳ookie涓繚瀛樹腑鏂囧彧鑳界紪鐮併€備竴鑸娇鐢║TF-8缂栫爜鍗冲彲銆備笉鎺ㄨ崘浣跨敤GBK绛変腑鏂囩紪鐮侊紝鍥犱负娴忚鍣ㄤ笉涓€瀹氭敮鎸侊紝鑰屼笖JavaScript涔熶笉鏀寔GBK缂栫爜銆?/p>
1.1.5 BASE64缂栫爜锛氫繚瀛樹簩杩涘埗鍥剧墖
Cookie涓嶄粎鍙互浣跨敤ASCII瀛楃涓嶶nicode瀛楃锛岃繕鍙互浣跨敤浜岃繘鍒舵暟鎹€備緥濡傚湪Cookie涓娇鐢ㄦ暟瀛楄瘉涔︼紝鎻愪緵瀹夊叏搴︺€備娇鐢ㄤ簩杩涘埗鏁版嵁鏃朵篃闇€瑕佽繘琛岀紪鐮併€?/p>
%娉ㄦ剰锛氭湰绋嬪簭浠呯敤浜庡睍绀篊ookie涓彲浠ュ瓨鍌ㄤ簩杩涘埗鍐呭锛屽苟涓嶅疄鐢ㄣ€傜敱浜庢祻瑙堝櫒姣忔璇锋眰鏈嶅姟鍣ㄩ兘浼氭惡甯ookie锛屽洜姝ookie鍐呭涓嶅疁杩囧锛屽惁鍒欏奖鍝嶉€熷害銆侰ookie鐨勫唴瀹瑰簲璇ュ皯鑰岀簿銆?/p>
1.1.6 璁剧疆Cookie鐨勬墍鏈夊睘鎬?/strong>
闄や簡name涓巚alue涔嬪锛孋ookie杩樺叿鏈夊叾浠栧嚑涓父鐢ㄧ殑灞炴€с€傛瘡涓睘鎬у搴斾竴涓猤etter鏂规硶涓庝竴涓猻etter鏂规硶銆侰ookie绫荤殑鎵€鏈夊睘鎬у琛?.1鎵€绀恒€?/p>
琛?.1 Cookie甯哥敤灞炴€?/p>
灞?nbsp; 鎬?nbsp; 鍚?/p>
|
鎻?nbsp; 杩?/p>
|
String name
|
璇ookie鐨勫悕绉般€侰ookie涓€鏃﹀垱寤猴紝鍚嶇О渚夸笉鍙洿鏀?/p>
|
Object value
|
璇ookie鐨勫€笺€傚鏋滃€间负Unicode瀛楃锛岄渶瑕佷负瀛楃缂栫爜銆傚鏋滃€间负浜岃繘鍒舵暟鎹紝鍒欓渶瑕佷娇鐢˙ASE64缂栫爜
|
int maxAge
|
璇ookie澶辨晥鐨勬椂闂达紝鍗曚綅绉掋€傚鏋滀负姝f暟锛屽垯璇ookie鍦╩axAge绉掍箣鍚庡け鏁堛€傚鏋滀负璐熸暟锛岃Cookie涓轰复鏃禖ookie锛屽叧闂祻瑙堝櫒鍗冲け鏁堬紝娴忚鍣ㄤ篃涓嶄細浠ヤ换浣曞舰寮忎繚瀛樿Cookie銆傚鏋滀负0锛岃〃绀哄垹闄よCookie銆傞粯璁や负–1
|
boolean secure
|
璇ookie鏄惁浠呰浣跨敤瀹夊叏鍗忚浼犺緭銆傚畨鍏ㄥ崗璁€傚畨鍏ㄥ崗璁湁HTTPS锛孲SL绛夛紝鍦ㄧ綉缁滀笂浼犺緭鏁版嵁涔嬪墠鍏堝皢鏁版嵁鍔犲瘑銆傞粯璁や负false
|
String path
|
璇ookie鐨勪娇鐢ㄨ矾寰勩€傚鏋滆缃负“/sessionWeb/”锛屽垯鍙湁contextPath涓?ldquo;/sessionWeb”鐨勭▼搴忓彲浠ヨ闂Cookie銆傚鏋滆缃负“/”锛屽垯鏈煙鍚嶄笅contextPath閮藉彲浠ヨ闂Cookie銆傛敞鎰忔渶鍚庝竴涓瓧绗﹀繀椤讳负“/”
|
String domain
|
鍙互璁块棶璇ookie鐨勫煙鍚嶃€傚鏋滆缃负“.google.com”锛屽垯鎵€鏈変互“google.com”缁撳熬鐨勫煙鍚嶉兘鍙互璁块棶璇ookie銆傛敞鎰忕涓€涓瓧绗﹀繀椤讳负“.”
|
String comment
|
璇ookie鐨勭敤澶勮鏄庛€傛祻瑙堝櫒鏄剧ずCookie淇℃伅鐨勬椂鍊欐樉绀鸿璇存槑
|
int version
|
璇ookie浣跨敤鐨勭増鏈彿銆?琛ㄧず閬靛惊Netscape鐨凜ookie瑙勮寖锛?琛ㄧず閬靛惊W3C鐨凴FC 2109瑙勮寖
|
1.1.7 Cookie鐨勬湁鏁堟湡
Cookie鐨刴axAge鍐冲畾鐫€Cookie鐨勬湁鏁堟湡锛屽崟浣嶄负绉掞紙Second锛夈€侰ookie涓€氳繃getMaxAge()鏂规硶涓巗etMaxAge(int maxAge)鏂规硶鏉ヨ鍐檓axAge灞炴€с€?/p>
濡傛灉maxAge灞炴€т负姝f暟锛屽垯琛ㄧず璇ookie浼氬湪maxAge绉掍箣鍚庤嚜鍔ㄥけ鏁堛€傛祻瑙堝櫒浼氬皢maxAge涓烘鏁扮殑Cookie鎸佷箙鍖栵紝鍗冲啓鍒板搴旂殑Cookie鏂囦欢涓€傛棤璁哄鎴峰叧闂簡娴忚鍣ㄨ繕鏄數鑴戯紝鍙杩樺湪maxAge绉掍箣鍓嶏紝鐧诲綍缃戠珯鏃惰Cookie浠嶇劧鏈夋晥銆備笅闈唬鐮佷腑鐨凜ookie淇℃伅灏嗘案杩滄湁鏁堛€?/p>
Cookie cookie = new Cookie("username","helloweenvsfei"); // 鏂板缓Cookie
cookie.setMaxAge(Integer.MAX_VALUE); // 璁剧疆鐢熷懡鍛ㄦ湡涓篗AX_VALUE
response.addCookie(cookie); // 杈撳嚭鍒板鎴风
濡傛灉maxAge涓鸿礋鏁帮紝鍒欒〃绀鸿Cookie浠呭湪鏈祻瑙堝櫒绐楀彛浠ュ強鏈獥鍙f墦寮€鐨勫瓙绐楀彛鍐呮湁鏁堬紝鍏抽棴绐楀彛鍚庤Cookie鍗冲け鏁堛€俶axAge涓鸿礋鏁扮殑Cookie锛屼负涓存椂鎬ookie锛屼笉浼氳鎸佷箙鍖栵紝涓嶄細琚啓鍒癈ookie鏂囦欢涓€侰ookie淇℃伅淇濆瓨鍦ㄦ祻瑙堝櫒鍐呭瓨涓紝鍥犳鍏抽棴娴忚鍣ㄨCookie灏辨秷澶变簡銆侰ookie榛樿鐨刴axAge鍊间负–1銆?/p>
濡傛灉maxAge涓?锛屽垯琛ㄧず鍒犻櫎璇ookie銆侰ookie鏈哄埗娌℃湁鎻愪緵鍒犻櫎Cookie鐨勬柟娉曪紝鍥犳閫氳繃璁剧疆璇ookie鍗虫椂澶辨晥瀹炵幇鍒犻櫎Cookie鐨勬晥鏋溿€傚け鏁堢殑Cookie浼氳娴忚鍣ㄤ粠Cookie鏂囦欢鎴栬€呭唴瀛樹腑鍒犻櫎锛?/p>
渚嬪锛?/p>
Cookie cookie = new Cookie("username","helloweenvsfei"); // 鏂板缓Cookie
cookie.setMaxAge(0); // 璁剧疆鐢熷懡鍛ㄦ湡涓?锛屼笉鑳戒负璐熸暟
response.addCookie(cookie); // 蹇呴』鎵ц杩欎竴鍙?/p>
response瀵硅薄鎻愪緵鐨凜ookie鎿嶄綔鏂规硶鍙湁涓€涓坊鍔犳搷浣渁dd(Cookie cookie)銆?/p>
瑕佹兂淇敼Cookie鍙兘浣跨敤涓€涓悓鍚嶇殑Cookie鏉ヨ鐩栧師鏉ョ殑Cookie锛岃揪鍒颁慨鏀圭殑鐩殑銆傚垹闄ゆ椂鍙渶瑕佹妸maxAge淇敼涓?鍗冲彲銆?/p>
娉ㄦ剰锛氫粠瀹㈡埛绔鍙朇ookie鏃讹紝鍖呮嫭maxAge鍦ㄥ唴鐨勫叾浠栧睘鎬ч兘鏄笉鍙鐨勶紝涔熶笉浼氳鎻愪氦銆傛祻瑙堝櫒鎻愪氦Cookie鏃跺彧浼氭彁浜ame涓巚alue灞炴€с€俶axAge灞炴€у彧琚祻瑙堝櫒鐢ㄦ潵鍒ゆ柇Cookie鏄惁杩囨湡銆?/p>
1.1.8 Cookie鐨勪慨鏀广€佸垹闄?/strong>
Cookie骞朵笉鎻愪緵淇敼銆佸垹闄ゆ搷浣溿€傚鏋滆淇敼鏌愪釜Cookie锛屽彧闇€瑕佹柊寤轰竴涓悓鍚嶇殑Cookie锛屾坊鍔犲埌response涓鐩栧師鏉ョ殑Cookie銆?/p>
濡傛灉瑕佸垹闄ゆ煇涓狢ookie锛屽彧闇€瑕佹柊寤轰竴涓悓鍚嶇殑Cookie锛屽苟灏唌axAge璁剧疆涓?锛屽苟娣诲姞鍒皉esponse涓鐩栧師鏉ョ殑Cookie銆傛敞鎰忔槸0鑰屼笉鏄礋鏁般€傝礋鏁颁唬琛ㄥ叾浠栫殑鎰忎箟銆傝鑰呭彲浠ラ€氳繃涓婁緥鐨勭▼搴忚繘琛岄獙璇侊紝璁剧疆涓嶅悓鐨勫睘鎬с€?/p>
娉ㄦ剰锛氫慨鏀广€佸垹闄ookie鏃讹紝鏂板缓鐨凜ookie闄alue銆乵axAge涔嬪鐨勬墍鏈夊睘鎬э紝渚嬪name銆乸ath銆乨omain绛夛紝閮借涓庡師Cookie瀹屽叏涓€鏍枫€傚惁鍒欙紝娴忚鍣ㄥ皢瑙嗕负涓や釜涓嶅悓鐨凜ookie涓嶄簣瑕嗙洊锛屽鑷翠慨鏀广€佸垹闄ゅけ璐ャ€?/p>
1.1.9 Cookie鐨勫煙鍚?/strong>
Cookie鏄笉鍙法鍩熷悕鐨勩€傚煙鍚峸ww.google.com棰佸彂鐨凜ookie涓嶄細琚彁浜ゅ埌鍩熷悕www.baidu.com鍘汇€傝繖鏄敱Cookie鐨勯殣绉佸畨鍏ㄦ満鍒跺喅瀹氱殑銆傞殣绉佸畨鍏ㄦ満鍒惰兘澶熺姝㈢綉绔欓潪娉曡幏鍙栧叾浠栫綉绔欑殑Cookie銆?/p>
姝e父鎯呭喌涓嬶紝鍚屼竴涓竴绾у煙鍚嶄笅鐨勪袱涓簩绾у煙鍚嶅www.helloweenvsfei.com鍜宨mages.helloweenvsfei.com涔熶笉鑳戒氦浜掍娇鐢–ookie锛屽洜涓轰簩鑰呯殑鍩熷悕骞朵笉涓ユ牸鐩稿悓銆傚鏋滄兂鎵€鏈塰elloweenvsfei.com鍚嶄笅鐨勪簩绾у煙鍚嶉兘鍙互浣跨敤璇ookie锛岄渶瑕佽缃瓹ookie鐨刣omain鍙傛暟锛屼緥濡傦細
Cookie cookie = new Cookie("time","20080808"); // 鏂板缓Cookie
cookie.setDomain(".helloweenvsfei.com"); // 璁剧疆鍩熷悕
cookie.setPath("/"); // 璁剧疆璺緞
cookie.setMaxAge(Integer.MAX_VALUE); // 璁剧疆鏈夋晥鏈?/p>
response.addCookie(cookie); // 杈撳嚭鍒板鎴风
璇昏€呭彲浠ヤ慨鏀规湰鏈篊:\WINDOWS\system32\drivers\etc涓嬬殑hosts鏂囦欢鏉ラ厤缃涓复鏃跺煙鍚嶏紝鐒跺悗浣跨敤setCookie.jsp绋嬪簭鏉ヨ缃法鍩熷悕Cookie楠岃瘉domain灞炴€с€?/p>
娉ㄦ剰锛歞omain鍙傛暟蹇呴』浠ョ偣(".")寮€濮嬨€傚彟澶栵紝name鐩稿悓浣哾omain涓嶅悓鐨勪袱涓狢ookie鏄袱涓笉鍚岀殑Cookie銆傚鏋滄兂瑕佷袱涓煙鍚嶅畬鍏ㄤ笉鍚岀殑缃戠珯鍏辨湁Cookie锛屽彲浠ョ敓鎴愪袱涓狢ookie锛宒omain灞炴€у垎鍒负涓や釜鍩熷悕锛岃緭鍑哄埌瀹㈡埛绔€?/p>
1.1.10 Cookie鐨勮矾寰?/strong>
domain灞炴€у喅瀹氳繍琛岃闂瓹ookie鐨勫煙鍚嶏紝鑰宲ath灞炴€у喅瀹氬厑璁歌闂瓹ookie鐨勮矾寰勶紙ContextPath锛夈€備緥濡傦紝濡傛灉鍙厑璁?sessionWeb/涓嬬殑绋嬪簭浣跨敤Cookie锛屽彲浠ヨ繖涔堝啓锛?/p>
Cookie cookie = new Cookie("time","20080808"); // 鏂板缓Cookie
cookie.setPath("/session/"); // 璁剧疆璺緞
response.addCookie(cookie); // 杈撳嚭鍒板鎴风
璁剧疆涓?ldquo;/”鏃跺厑璁告墍鏈夎矾寰勪娇鐢–ookie銆俻ath灞炴€ч渶瑕佷娇鐢ㄧ鍙?ldquo;/”缁撳熬銆俷ame鐩稿悓浣哾omain鐩稿悓鐨勪袱涓狢ookie涔熸槸涓や釜涓嶅悓鐨凜ookie銆?/p>
娉ㄦ剰锛氶〉闈㈠彧鑳借幏鍙栧畠灞炰簬鐨凱ath鐨凜ookie銆備緥濡?session/test/a.jsp涓嶈兘鑾峰彇鍒拌矾寰勪负/session/abc/鐨凜ookie銆備娇鐢ㄦ椂涓€瀹氳娉ㄦ剰銆?/p>
1.1.11 Cookie鐨勫畨鍏ㄥ睘鎬?/strong>
HTTP鍗忚涓嶄粎鏄棤鐘舵€佺殑锛岃€屼笖鏄笉瀹夊叏鐨勩€備娇鐢℉TTP鍗忚鐨勬暟鎹笉缁忚繃浠讳綍鍔犲瘑灏辩洿鎺ュ湪缃戠粶涓婁紶鎾紝鏈夎鎴幏鐨勫彲鑳姐€備娇鐢℉TTP鍗忚浼犺緭寰堟満瀵嗙殑鍐呭鏄竴绉嶉殣鎮c€傚鏋滀笉甯屾湜Cookie鍦℉TTP绛夐潪瀹夊叏鍗忚涓紶杈擄紝鍙互璁剧疆Cookie鐨剆ecure灞炴€т负true銆傛祻瑙堝櫒鍙細鍦℉TTPS鍜孲SL绛夊畨鍏ㄥ崗璁腑浼犺緭姝ょ被Cookie銆備笅闈㈢殑浠g爜璁剧疆secure灞炴€т负true锛?/p>
Cookie cookie = new Cookie("time", "20080808"); // 鏂板缓Cookie
cookie.setSecure(true); // 璁剧疆瀹夊叏灞炴€?/strong>
response.addCookie(cookie); // 杈撳嚭鍒板鎴风
鎻愮ず锛歴ecure灞炴€у苟涓嶈兘瀵笴ookie鍐呭鍔犲瘑锛屽洜鑰屼笉鑳戒繚璇佺粷瀵圭殑瀹夊叏鎬с€傚鏋滈渶瑕侀珮瀹夊叏鎬э紝闇€瑕佸湪绋嬪簭涓Cookie鍐呭鍔犲瘑銆佽В瀵嗭紝浠ラ槻娉勫瘑銆?/strong>
1.1.12 JavaScript鎿嶄綔Cookie
Cookie鏄繚瀛樺湪娴忚鍣ㄧ鐨勶紝鍥犳娴忚鍣ㄥ叿鏈夋搷浣淐ookie鐨勫厛鍐虫潯浠躲€傛祻瑙堝櫒鍙互浣跨敤鑴氭湰绋嬪簭濡侸avaScript鎴栬€匳BScript绛夋搷浣淐ookie銆傝繖閲屼互JavaScript涓轰緥浠嬬粛甯哥敤鐨凜ookie鎿嶄綔銆備緥濡備笅闈㈢殑浠g爜浼氳緭鍑烘湰椤甸潰鎵€鏈夌殑Cookie銆?/p>
<script>document.write(document.cookie);</script>
鐢变簬JavaScript鑳藉浠绘剰鍦拌鍐機ookie锛屾湁浜涘ソ浜嬭€呬究鎯充娇鐢↗avaScript绋嬪簭鍘荤鎺㈢敤鎴峰湪鍏朵粬缃戠珯鐨凜ookie銆備笉杩囪繖鏄緬鍔崇殑锛學3C缁勭粐鏃╁氨鎰忚瘑鍒癑avaScript瀵笴ookie鐨勮鍐欐墍甯︽潵鐨勫畨鍏ㄩ殣鎮e苟鍔犱互闃插浜嗭紝W3C鏍囧噯鐨勬祻瑙堝櫒浼氶樆姝avaScript璇诲啓浠讳綍涓嶅睘浜庤嚜宸辩綉绔欑殑Cookie銆傛崲鍙ヨ瘽璇达紝A缃戠珯鐨凧avaScript绋嬪簭璇诲啓B缃戠珯鐨凜ookie涓嶄細鏈変换浣曠粨鏋溿€?/p>
1.1.13 妗堜緥锛氭案涔呯櫥褰?/strong>
濡傛灉鐢ㄦ埛鏄湪鑷繁瀹剁殑鐢佃剳涓婁笂缃戯紝鐧诲綍鏃跺氨鍙互璁颁綇浠栫殑鐧诲綍淇℃伅锛屼笅娆¤闂椂涓嶉渶瑕佸啀娆$櫥褰曪紝鐩存帴璁块棶鍗冲彲銆傚疄鐜版柟娉曟槸鎶婄櫥褰曚俊鎭璐﹀彿銆佸瘑鐮佺瓑淇濆瓨鍦–ookie涓紝骞舵帶鍒禖ookie鐨勬湁鏁堟湡锛屼笅娆¤闂椂鍐嶉獙璇丆ookie涓殑鐧诲綍淇℃伅鍗冲彲銆?/strong>
淇濆瓨鐧诲綍淇℃伅鏈夊绉嶆柟妗堛€傛渶鐩存帴鐨勬槸鎶婄敤鎴峰悕涓庡瘑鐮侀兘淇濇寔鍒癈ookie涓紝涓嬫璁块棶鏃舵鏌ookie涓殑鐢ㄦ埛鍚嶄笌瀵嗙爜锛屼笌鏁版嵁搴撴瘮杈冦€傝繖鏄?strong>涓€绉嶆瘮杈冨嵄闄╃殑閫夋嫨锛屼竴鑸笉鎶婂瘑鐮佺瓑閲嶈淇℃伅淇濆瓨鍒癈ookie涓?/strong>銆?/p>
杩樻湁涓€绉嶆柟妗堟槸鎶婂瘑鐮佸姞瀵嗗悗淇濆瓨鍒癈ookie涓紝涓嬫璁块棶鏃惰В瀵嗗苟涓庢暟鎹簱姣旇緝銆傝繖绉嶆柟妗堢暐寰畨鍏ㄤ竴浜涖€傚鏋滀笉甯屾湜淇濆瓨瀵嗙爜锛岃繕鍙互鎶婄櫥褰曠殑鏃堕棿鎴充繚瀛樺埌Cookie涓庢暟鎹簱涓紝鍒版椂鍙獙璇佺敤鎴峰悕涓庣櫥褰曟椂闂存埑灏卞彲浠ヤ簡銆?/p>
杩欏嚑绉嶆柟妗堥獙璇佽处鍙锋椂閮借鏌ヨ鏁版嵁搴撱€?/p>
鏈緥灏嗛噰鐢ㄥ彟涓€绉嶆柟妗堬紝鍙湪鐧诲綍鏃舵煡璇竴娆℃暟鎹簱锛屼互鍚庤闂獙璇佺櫥褰曚俊鎭椂涓嶅啀鏌ヨ鏁版嵁搴撱€傚疄鐜版柟寮忔槸鎶婅处鍙锋寜鐓т竴瀹氱殑瑙勫垯鍔犲瘑鍚庯紝杩炲悓璐﹀彿涓€鍧椾繚瀛樺埌Cookie涓€備笅娆¤闂椂鍙渶瑕佸垽鏂处鍙风殑鍔犲瘑瑙勫垯鏄惁姝g‘鍗冲彲銆傛湰渚嬫妸璐﹀彿淇濆瓨鍒板悕涓篴ccount鐨凜ookie涓紝鎶婅处鍙疯繛鍚屽瘑閽ョ敤MD1绠楁硶鍔犲瘑鍚庝繚瀛樺埌鍚嶄负ssid鐨凜ookie涓€傞獙璇佹椂楠岃瘉Cookie涓殑璐﹀彿涓庡瘑閽ュ姞瀵嗗悗鏄惁涓嶤ookie涓殑ssid鐩哥瓑銆傜浉鍏充唬鐮佸涓嬶細
浠g爜1.8 loginCookie.jsp
<%@ page language="java"pageEncoding="UTF-8" isErrorPage="false" %>
<%! // JSP鏂规硶
private static final String KEY =":[email protected]";
// 瀵嗛挜
public final static String calcMD1(Stringss) { // MD1 鍔犲瘑绠楁硶
String s = ss==null ?"" : ss; // 鑻ヤ负null杩斿洖绌?/p>
char hexDigits[] = { 鈥?鈥?鈥?鈥? 鈥?鈥? 鈥?鈥? 鈥?鈥? 鈥?鈥? 鈥?鈥? 鈥?鈥? 鈥?鈥? 鈥?鈥?
鈥榓鈥? 鈥榖鈥? 鈥榗鈥? 鈥榙鈥? 鈥榚鈥? 鈥榝鈥?}; // 瀛楀吀
try {
byte[] strTemp =s.getBytes(); // 鑾峰彇瀛楄妭
MessageDigestmdTemp = MessageDigest.getInstance("MD1"); // 鑾峰彇MD1
mdTemp.update(strTemp); // 鏇存柊鏁版嵁
byte[] md =mdTemp.digest(); // 鍔犲瘑
int j =md.length; // 鍔犲瘑鍚庣殑闀垮害
char str[] = newchar[j * 2]; // 鏂板瓧绗︿覆鏁扮粍
int k =0; // 璁℃暟鍣╧
for (int i = 0; i< j; i++) { // 寰幆杈撳嚭
byte byte0 =md[i];
str[k++] =hexDigits[byte0 >>> 4 & 0xf];
str[k++] =hexDigits[byte0 & 0xf];
}
return newString(str); // 鍔犲瘑鍚庡瓧绗︿覆
} catch (Exception e){return null; }
}
%>
<%
request.setCharacterEncoding("UTF-8"); // 璁剧疆request缂栫爜
response.setCharacterEncoding("UTF-8"); // 璁剧疆response缂栫爜
String action =request.getParameter("action"); // 鑾峰彇action鍙傛暟
if("login".equals(action)){ // 濡傛灉涓簂ogin鍔ㄤ綔
String account =request.getParameter("account");
// 鑾峰彇account鍙傛暟
String password =request.getParameter("password");
// 鑾峰彇password鍙傛暟
int timeout = newInteger(request.getParameter("timeout"));
// 鑾峰彇timeout鍙傛暟
String ssid =calcMD1(account + KEY); // 鎶婅处鍙枫€佸瘑閽ヤ娇鐢∕D1鍔犲瘑鍚庝繚瀛?/p>
CookieaccountCookie = new Cookie("account", account);
// 鏂板缓Cookie
accountCookie.setMaxAge(timeout); // 璁剧疆鏈夋晥鏈?/p>
Cookie ssidCookie =new Cookie("ssid", ssid); // 鏂板缓Cookie
ssidCookie.setMaxAge(timeout); // 璁剧疆鏈夋晥鏈?/p>
response.addCookie(accountCookie); // 杈撳嚭鍒板鎴风
response.addCookie(ssidCookie); // 杈撳嚭鍒板鎴风
// 閲嶆柊璇锋眰鏈〉闈紝鍙傛暟涓甫鏈夋椂闂存埑锛岀姝㈡祻瑙堝櫒缂撳瓨椤甸潰鍐呭
response.sendRedirect(request.getRequestURI() + "?" + System.
currentTimeMillis());
return;
}
elseif("logout".equals(action)){ // 濡傛灉涓簂ogout鍔ㄤ綔
CookieaccountCookie = new Cookie("account", "");
// 鏂板缓Cookie锛屽唴瀹逛负绌?/p>
accountCookie.setMaxAge(0); // 璁剧疆鏈夋晥鏈熶负0锛屽垹闄?/p>
Cookie ssidCookie =new Cookie("ssid", ""); // 鏂板缓Cookie锛屽唴瀹逛负绌?/p>
ssidCookie.setMaxAge(0); // 璁剧疆鏈夋晥鏈熶负0锛屽垹闄?/p>
response.addCookie(accountCookie); // 杈撳嚭鍒板鎴风
response.addCookie(ssidCookie); // 杈撳嚭鍒板鎴风
//閲嶆柊璇锋眰鏈〉闈紝鍙傛暟涓甫鏈夋椂闂存埑锛岀姝㈡祻瑙堝櫒缂撳瓨椤甸潰鍐呭
response.sendRedirect(request.getRequestURI() + "?" + System.
currentTimeMillis());
return;
}
boolean login = false; // 鏄惁鐧诲綍
String account = null; // 璐﹀彿
String ssid = null; // SSID鏍囪瘑
if(request.getCookies() !=null){ // 濡傛灉Cookie涓嶄负绌?/p>
for(Cookie cookie :request.getCookies()){ // 閬嶅巻Cookie
if(cookie.getName().equals("account")) // 濡傛灉Cookie鍚嶄负
account
account = cookie.getValue(); // 淇濆瓨account鍐呭
if(cookie.getName().equals("ssid")) // 濡傛灉涓篠SID
ssid = cookie.getValue(); // 淇濆瓨SSID鍐呭
}
}
if(account != null && ssid !=null){ // 濡傛灉account銆丼SID閮戒笉涓虹┖
login =ssid.equals(calcMD1(account + KEY));
// 濡傛灉鍔犲瘑瑙勫垯姝g‘, 鍒欒涓哄凡缁忕櫥褰?/p>
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN">
<legend><%= login ? "娆㈣繋鎮ㄥ洖鏉? : "璇峰厛鐧诲綍"%></legend>
<% if(login){%>
娆㈣繋鎮? ${ cookie.account.value }.
<a href="${ pageContext.request.requestURI }?action=logout">
娉ㄩ攢</a>
<% } else {%>
<formaction="${ pageContext.request.requestURI }?action=login"
method="post">
<table>
<tr><td>璐﹀彿锛?</td>
<td><input type="text"name="account" style="width:
200px; "></td>
</tr>
<tr><td>瀵嗙爜锛?</td>
<td><inputtype="password" name="password"></td>
</tr>
<tr>
<td>鏈夋晥鏈燂細 </td>
<td><inputtype="radio" name="timeout" value="-1"
checked> 鍏抽棴娴忚鍣ㄥ嵆澶辨晥 <br/> <input type="radio"
name="timeout" value="<%= 30 *24 * 60 * 60 %>"> 30澶?br /> 鍐呮湁鏁?<br/><input type="radio" name="timeout" value=
"<%= Integer.MAX_VALUE %>"> 姘镐箙鏈夋晥 <br/> </td> </tr>
<tr><td></td>
<td><input type="submit"value=" 鐧?nbsp; 褰?" class=
"button"></td>
</tr>
</table>
</form>
<% } %>
鐧诲綍鏃跺彲浠ラ€夋嫨鐧诲綍淇℃伅鐨勬湁鏁堟湡锛氬叧闂祻瑙堝櫒鍗冲け鏁堛€?0澶╁唴鏈夋晥涓庢案涔呮湁鏁堛€傞€氳繃璁剧疆Cookie鐨刟ge灞炴€ф潵瀹炵幇锛屾敞鎰忚瀵熶唬鐮併€傝繍琛屾晥鏋滃鍥?.7鎵€绀恒€?/p>
鍥?.7 姘镐箙鐧诲綍
鎻愮ず锛氳鍔犲瘑鏈哄埗涓渶閲嶈鐨勯儴鍒嗕负绠楁硶涓庡瘑閽ャ€傜敱浜嶮D1绠楁硶鐨勪笉鍙€嗘€э紝鍗充娇鐢ㄦ埛鐭ラ亾浜嗚处鍙蜂笌鍔犲瘑鍚庣殑瀛楃涓诧紝涔熶笉鍙兘瑙e瘑寰楀埌瀵嗛挜銆傚洜姝わ紝鍙淇濈濂藉瘑閽ヤ笌绠楁硶锛岃鏈哄埗灏辨槸瀹夊叏鐨勩€?/p>
1.2 Session鏈哄埗
闄や簡浣跨敤Cookie锛學eb搴旂敤绋嬪簭涓繕缁忓父浣跨敤Session鏉ヨ褰曞鎴风鐘舵€併€?strong>Session鏄湇鍔″櫒绔娇鐢ㄧ殑涓€绉嶈褰曞鎴风鐘舵€佺殑鏈哄埗
锛屼娇鐢ㄤ笂姣擟ookie绠€鍗曚竴浜涳紝鐩稿簲鐨勪篃