鎬庢牱鍒ゆ柇鐢ㄦ埛鏄惁鍦ㄧ嚎
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎬庢牱鍒ゆ柇鐢ㄦ埛鏄惁鍦ㄧ嚎相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
1.鍐欏悗鍙扮嚎绋嬶紝蹇冭烦鏈哄埗绛夊垽鏂敤鎴锋槸鍚﹀湪绾裤€?/p>
2.鍒╃敤session 姣忔鐢ㄦ埛鐧诲綍浼氫骇鐢熶竴涓猻ession
鍥犱负鑰冭檻鍒颁竴鍙扮數鑴戝涓祻瑙堝櫒涔嬮棿session涓嶅叡浜篃灏辨槸鍙兘浼氬舰鎴愯繖鏍蜂竴涓蹇碉紝浣犵殑鍚屼竴涓敤鎴峰悕鍦ㄦ湇鍔″櫒涓墦寮€浜嗕袱涓笉鍚岀増鏈殑娴忚鍣ㄥ洜姝や繚瀛樹簡2涓猻ession锛屼篃灏遍€犳垚浜嗕笉鍑嗙‘鐨勫洜绱?br />鎵€浠ヤ綘鍙互閲囩敤涓€寮犱笓闂ㄧ櫥褰曠殑鏁版嵁搴撹〃鏍硷紝瀛楁涓嶇敤澶?nbsp;淇濆瓨鐧诲綍鐨勭敤鎴峰悕灏辫锛岀劧鍚巎avaweb涓湁session鐨勭洃鍚帴鍙ttpSessionBindingListener锛岃繖涓帴鍙e寘鍚殑涓や釜鏂规硶鍒嗗埆浼氬湪姣忔鍥犵敤鎴风敓鎴愪竴涓猻ession瑙﹀彂锛屼互鍙婂洜鐢ㄦ埛姝e父娉ㄩ攢session鎴栭潪娉曡秴鏃舵湇鍔″櫒鎽ф瘉session鍚庤Е鍙戯紝
鎴戜滑瑕佸仛鐨勭粏鍖栧伐浣滃氨鏄紝褰撴瘡涓敤鎴蜂骇鐢熶竴娆ession鏃讹紙鐧诲綍鎿嶄綔锛夛紝鏌ユ壘杩欏紶琛ㄦ牸鏄惁宸叉湁姝よ处鍙风殑鐧诲綍璁板綍銆傛湁鍒欏湪姝ょ櫥褰曡〃涓笉鍋氬鍔犳搷浣滐紝娌℃湁鍒欏鍔犺鐢ㄦ埛鍒版鐧诲綍琛ㄣ€?br />杩欐牱鍙互瀹炵幇鐩稿绮惧噯鐨勭櫥褰曚汉鏁帮紝浣嗗闈炴硶閫€鍑虹殑鐢ㄦ埛锛屾垜鎯宠嚦浠婁篃娌℃湁涓€涓ソ鐨勬兂娉曪紝杩欑鐧诲綍浼氬瀛樺湪浜庢湇鍔″櫒锛岀煡閬撴湇鍔″櫒鍒ゅ畾璇ession瓒呮椂鐒跺悗鍒犻櫎锛岃鐢ㄦ埛涔熷氨鑷姩琚垹闄ょ櫥褰曡〃銆?/p>
4.鍙互鐢╝pplication璁$畻鍦ㄧ嚎浜烘暟锛屾瘮杈冨ソ鍐?/p>
5.session鐨勯攢姣佸拰鍒涘缓鏄彲浠ョ洃鍚殑銆?br />鍦╝pplication瀹瑰櫒涓淮鎶や竴涓彉閲忋€傚綋鐩戝惉瑕乻ession鍒涘缓鑰呭姞涓€鍙嶄箣鍑忎竴
瀹瑰櫒锛屼笉鏄獥鍙f墦閿欎簡
JavaWeb涓殑Session銆丼essionListener銆佸湪绾夸汉鏁扮粺璁?/a>
5.
娴嬭瘯鐜锛歍omcat 5.0.28
JavaWeb涓殑Session
- 鍦↗avaWeb涓娇鐢℉ttpSession锛堜互涓嬬畝绉皊ession锛夊璞℃潵琛ㄧず涓€涓細璇濄€?/li>
- Session鐨勫垱寤猴紙浠h〃浼氳瘽鍛ㄦ湡鐨勫紑濮嬶級锛氱涓€娆¤皟鐢╮equest.getSession()鏂规硶鏃讹紝浼氬垱寤轰竴涓猻ession瀵硅薄銆?/li>
- Session鐨勯攢姣侊紙浠h〃浼氳瘽鍛ㄦ湡鐨勭粨鏉燂級锛氬湪鏌愪釜璇锋眰鍛ㄦ湡鍐呰皟鐢ㄤ簡Session.invalidate()鏂规硶锛屾璇锋眰鍛ㄦ湡缁撴潫鍚庯紝session琚攢姣侊紱鎴栬€呮槸session瓒呮椂鍚庤嚜鍔ㄩ攢姣併€?/li>
- 瀵逛簬JSP锛屽鏋滄寚瀹氫簡<%@ page session="false"%>锛屽垯鍦↗SP涓棤娉曠洿鎺ヨ闂唴缃殑session鍙橀噺锛屽悓鏃朵篃涓嶄細涓诲姩鍒涘缓session锛屽洜涓烘鏃禞SP鏈嚜鍔ㄦ墽琛宺equest.getSession()鎿嶄綔鑾峰彇session銆?/li>
- 鍦╯ession瀵硅薄鍒氬垱寤虹殑绗竴涓姹傚懆鏈熷唴锛岃皟鐢╯ession.isNew()鏂规硶灏嗗緱鍒皌rue銆?/li>
- 鍙互鍦╳eb.xml鏂囦欢涓厤缃畇ession榛樿鐨勮秴鏃舵椂闂达紙鍒嗛挓锛夛細
<session-config>涔熷彲浠ヨ皟鐢╯ession. setMaxInactiveInterval()鏂规硶璁剧疆session瓒呮椂鏃堕棿锛堝垎閽燂級
<session-timeout>10</session-timeout>
</session-config>
SessionListener
- 閫氳繃SessionListenr鍙互鐩戝惉session鐨勫垱寤哄拰閿€姣侊紝姝ラ锛?br />1.鍐欎竴涓被MySessionListener锛屽疄鐜癹avax.servlet.http.HttpSessionListener鎺ュ彛鍙婂叾sessionCreated()銆乻essionDestroyed()鏂规硶
2.鍦╳eb.xml涓厤缃甋essionListener锛?br /><listener>
<listener-class>MySessionListener绫诲叏鍚?lt;/listener-class>
</listener> - 褰搒ession琚垱寤哄拰閿€姣佹椂锛屽鍣ㄤ細鍒嗗埆璋冪敤SessionListener鐨剆essionCreated()鏂规硶鍜宻essionDestroyed()鏂规硶锛岃繖涓や釜鏂规硶涓紶鍏ヤ簡涓€涓弬鏁板璞ttpSessionEvent锛屽彲浠ラ€氳繃姝ゅ璞$殑getSession()鏂规硶鑾峰彇session瀵硅薄銆?/li>
搴旂敤锛氬湪绾夸汉鏁扮粺璁?/strong>
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 鍦╝pplication鑼冨洿鐢变竴涓狧ashSet闆嗕繚瀛樻墍鏈夌殑session
HashSet sessions = (HashSet) application.getAttribute("sessions");
if (sessions == null) {
sessions = new HashSet();
application.setAttribute("sessions", sessions);
}
// 鏂板垱寤虹殑session鍧囨坊鍔犲埌HashSet闆嗕腑
sessions.add(session);
// 鍙互鍦ㄥ埆澶勪粠application鑼冨洿涓彇鍑簊essions闆嗗悎
}
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
HashSet sessions = (HashSet) application.getAttribute("sessions");
// 閿€姣佺殑session鍧囦粠HashSet闆嗕腑绉婚櫎
sessions.remove(session);
}
}
以上是关于鎬庢牱鍒ゆ柇鐢ㄦ埛鏄惁鍦ㄧ嚎的主要内容,如果未能解决你的问题,请参考以下文章