銆銆涓縐嶆槸欏甸潰鍒鋒柊鐢辯敤鎴鋒帶鍒訛紝鏈嶅姟鍣ㄧ鎺у埗涓涓秴鏃舵椂闂存瘮濡?0鍒嗛挓錛屽埌浜嗘椂闂翠箣鍚庣敤鎴鋒病鏈夊姩浣滃氨琚涪鍑恒傝繖縐嶆柟娉曠殑浼樼偣鏄紝濡傛灉鐢ㄦ埛蹇樹簡閫鍑猴紝鍙互闃叉鍒漢鎭舵剰鎿嶄綔銆傜己鐐規槸錛屽鏋滀綘鍦ㄥ仛涓浠跺緢鑰楁椂闂寸殑浜嬫儏錛岃秴榪囦簡榪欎釜鏃墮棿闄愬埗錛宻ubmit鐨勬椂鍊欏彲鑳借鍐嶆闈復鐧婚檰銆傚鏋滃師鏉ョ殑鍙墮潰鍙堟槸寮哄埗澶辨晥鐨勮瘽錛屽氨鏈夊彲鑳戒涪澶變綘鍋氱殑宸ヤ綔銆傚湪瀹炵幇鐨勮搴︽潵鐪嬶紝榪欐槸鏈綆鍗曠殑錛孲erver绔粯璁ゅ疄鐜扮殑灝辨槸榪欐牱鐨勬ā寮忋?/p>
銆銆鍙︿竴縐嶆柟寮忔槸錛岀珯鐐歸噰鐢ㄦ鏋剁粨鏋勶紝鏈変竴涓狥rame鎴栬呴殣钘忕殑iframe鍦ㄤ笉鏂埛鏂幫紝榪欐牱浣犳案榪滀笉浼氳韙㈠嚭錛屼絾鏄湇鍔″櫒绔負浜嗗垽鏂綘鏄惁鍦ㄧ嚎錛岄渶瑕佸畾涓涓彂鍛嗘椂闂達紝濡傛灉瓚呰繃榪欎釜鍙戝憜鏃墮棿浣犻櫎浜嗚繖涓嚜鍔ㄥ埛鏂扮殑欏甸潰澶栨病鏈夊埛鏂板叾浠栭〉闈㈢殑璇濓紝灝辮涓轟綘宸茬粡涓嶅湪綰夸簡銆傞噰鍙栬繖縐嶆柟寮忕殑鍏稿瀷鏄痻ici.net銆?浠栫殑浼樼偣鏄彲浠ュ彲浠ュ埄鐢ㄤ笉鏂殑鍒鋒柊瀹炵幇涓浜涚被浼約erver-push鐨勫姛鑳斤紝姣斿緗戝弸涔嬮棿鍙戦佹秷鎭?/p>
銆銆涓嶇鍝竴縐嶆ā寮忥紝涓轟簡瀹炵幇嫻忚褰撳墠鎵鏈夌殑鍦ㄧ嚎鐢ㄦ埛錛岃繕闇瑕佸仛涓浜涢澶栫殑宸ヤ綔銆俿ervlet API涓病鏈夊緱鍒癝ession鍒楄〃鐨凙PI銆?/p>
銆銆鍙互鍒╃敤鐨勬槸Listener. Servlet 2.2鍜?.3瑙勮寖鍦ㄨ繖閲岀暐寰湁涓浜涗笉涓鏍楓?.2涓璈ttpSessionBindingListener鍙互瀹炵幇褰撲竴涓狧TTPSession涓殑Attribute鍙樺寲鐨勬椂鍊欓氱煡浣犵殑綾匯傝?.3涓繕寮曞叆浜咹ttpSessionAttributeListener.閴翠簬鎴戜嬌鐢ㄧ殑鐜鏄疺isual age for java 4鍜孞Run server 3.1,浠栦滑榪樹笉鐩存帴鏀寔Servlet 2.3鐨勭紪紼嬶紝榪欓噷鎴戠敤鐨勬槸HttpSessionBindingListener.
銆銆闇瑕佸仛鐨勪簨鎯呭寘鎷仛涓涓柊鐨勭被鏉ュ疄鐜癏ttpSessionBindingListener鎺ュ彛銆傝繖涓帴鍙f湁涓や釜鏂規硶錛?/p>
public void valueBound(HttpSessionBindingEvent event)
public void valueUnbound(HttpSessionBindingEvent event)
銆銆褰撲綘鎵цSession.addAttribute(String,Object)鐨勬椂鍊欙紝濡傛灉浣犲凡緇忔妸涓涓疄鐜頒簡HttpSessionBindingListener鎺ュ彛鐨勭被鍔犲叆涓篈ttribute,Session浼氶氱煡浣犵殑綾伙紝璋冪敤浣犵殑valueBound鏂規硶銆傜浉鍙嶏紝Session.removeAttribute鏂規硶瀵瑰簲鐨勬槸valueUndound鏂規硶銆?/p>
public class HttpSessionBinding implements javax.servlet.http.HttpSessionBindingListener
{
銆ServletContext application = null;
銆public HttpSessionBinding(ServletContext application)
銆{
銆銆super();
銆銆if (application ==null)
銆銆銆throw new IllegalArgumentException("Null application is not accept.");
銆銆this.application = application;
銆}
銆public void valueBound(javax.servlet.http.HttpSessionBindingEvent e)
銆{
銆銆Vector activeSessions = (Vector) application.getAttribute("activeSessions");
銆銆if (activeSessions == null)
銆銆{
銆銆銆activeSessions = new Vector();
銆銆}
銆銆JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");
銆銆if (sessionUser != null)
銆銆{
銆銆銆activeSessions.add(e.getSession());
銆銆}
銆銆application.setAttribute("activeSessions",activeSessions);
銆}
銆public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent e)
銆{
銆銆JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");
銆銆if (sessionUser == null)
銆銆{
銆銆銆Vector activeSessions = (Vector) application.getAttribute("activeSessions");
銆銆銆if (activeSessions != null)
銆銆銆{
銆銆銆銆activeSessions.remove(e.getSession().getId());
銆銆銆銆application.setAttribute("activeSessions",activeSessions);
銆銆銆}
銆銆}
銆}
}
銆銆鍋囪鍏朵腑鐨凧DBCUser綾繪槸涓涓換鎰廢ser綾匯傚湪鎵ц鐢ㄦ埛鐧誨綍鏃訛紝鎶奤ser綾誨拰HttpSessionBinding綾婚兘鍔犲叆鍒癝ession涓幓銆?/p>
銆銆榪欐牱錛屾瘡嬈$敤鎴風櫥褰曞悗錛屽湪application涓殑attribute "activeSessions"榪欎釜vector涓兘浼氬鍔犱竴鏉¤褰曘傛瘡褰搒ession瓚呮椂錛寁alueUnbound琚Е鍙戯紝鍦ㄨ繖涓獀ector涓垹鍘誨皢瑕佽瓚呮椂鐨剆ession.
public void login()
throws ACLException,SQLException,IOException
{
銆/* get JDBC User Class */
銆if (user != null)
銆{
銆銆logout();
銆}
銆{
銆銆// if session time out, or user didn't login, save the target url temporary.
銆銆JDBCUserFactory uf = new JDBCUserFactory();
銆銆if ( (this.request.getParameter("userID")==null) || (this.request.getParameter("password")==null) )
銆銆{
銆銆銆throw new ACLException("Please input a valid userName and password.");
銆銆}
銆銆JDBCUser user = (JDBCUser) uf.UserLogin(
銆銆銆this.request.getParameter("userID"),
銆銆銆this.request.getParameter("password") );
銆銆銆user.touchLoginTime();
銆銆銆this.session.setAttribute("user",user);
銆銆銆this.session.setAttribute("BindingNotify",new HttpSessionBinding(application));
銆銆}
銆}
銆銆Login鐨勬椂鍊欙紝鎶奤ser鍜岃繖涓狟indingNotofy鐩殑鐨勭被閮藉姞鍏ュ埌session涓幓銆俵ogout鐨勬椂鍊欙紝灝辮涓誨姩鍦╝ctiveSessions榪欎釜vector涓垹鍘昏繖涓猻ession.
public void logout()
throws SQLException,ACLException
{
銆if (this.user == null && this.session.getAttribute("user")==null)
銆{
銆銆return;
銆}
銆Vector activeSessions = (Vector) this.application.getAttribute("activeSessions");
銆if (activeSessions != null)
銆{
銆銆activeSessions.remove(this.session);
銆銆application.setAttribute("activeSessions",activeSessions);
銆}
銆java.util.Enumeration e = this.session.getAttributeNames();
銆while (e.hasMoreElements())
銆{
銆銆String s = (String)e.nextElement();
銆銆this.session.removeAttribute(s);
銆}
銆this.user.touchLogoutTime();
銆this.user = null;
}
銆銆 榪欎袱涓嚱鏁頒綅浜庝竴涓狧ttpSessionManager綾諱腑.榪欎釜綾誨紩鐢ㄤ簡jsp閲岄潰鐨刟pplication鍏ㄥ眬瀵硅薄銆傝繖涓被鐨勫叾浠栦唬鐮佸拰鏈枃鏃犲叧涓旂浉褰撻暱錛屾垜灝變笉璐村嚭鏉ヤ簡銆?br>涓嬮潰鏉ョ湅鐪媕sp閲岄潰鎬庝箞鐢ㄣ?/p>
銆銆鍋囪涓涓櫥褰曠敤鐨勮〃鍗曡鎻愪氦鍒癲oLogin.jsp, 琛ㄥ崟涓寘鍚玌serName鍜宲assword鍩熴傝妭閫夐儴鍒嗙墖孌碉細
錛?
HttpSessionManager hsm = new HttpSessionManager(application,request,response);
try
{
銆hsm.login();
}
catch ( UserNotFoundException e)
{
銆response.sendRedirect("InsufficientPrivilege.jsp?detail=User%20does%20not%20exist.");
銆return;
}
catch ( InvalidPasswordException e2)
{
銆response.sendRedirect("InsufficientPrivilege.jsp?detail=Invalid%20Password");
銆return;
}
catch ( Exception e3)
{
銆%錛?Error:錛?=e3.toString() %錛烇紲br錛?br>銆Press 錛渁 href="login.jsp"錛濰ere錛?a錛?to relogin.
銆錛? return;
}
response.sendRedirect("index.jsp");
%錛?
銆銆鍐嶆潵鐪嬬湅鐜板湪鎴戜滑鎬庝箞寰楀埌涓涓綋鍓嶅湪綰跨殑鐢ㄦ埛鍒楄〃銆?/p>
錛渂ody bgcolor="#FFFFFF"錛?br>錛渢able cellspacing="0" cellpadding="0" width="100%"錛?/p>
錛渢r 錛?br>錛渢d style="width:24px"錛濻essionId
錛?td錛?br>錛渢d style="width:80px" 錛濽ser
錛?td錛?br>錛渢d style="width:80px" 錛濴ogin Time
錛?td錛?br>錛渢d style="width:80px" 錛濴ast access Time
錛?td錛?br>錛?tr錛?br>錛?
Vector activeSessions = (Vector) application.getAttribute("activeSessions");
if (activeSessions == null)
{
銆activeSessions = new Vector();
銆application.setAttribute("activeSessions",activeSessions);
}
Iterator it = activeSessions.iterator();
while (it.hasNext())
{
銆HttpSession sess = (HttpSession)it.next();
銆JDBCUser sessionUser = (JDBCUser)sess.getAttribute("user");
銆String userId = (sessionUser!=null)?sessionUser.getUserID():"None";
%錛?br>錛渢r錛?br>錛渢d nowrap=''錛烇紲%= sess.getId() %錛烇紲/td錛?br>錛渢d nowrap=''錛烇紲%= userId %錛烇紲/td錛?br>錛渢d nowrap=''錛?br>錛?= BeaconDate.getInstance( new java.util.Date(sess.getCreationTime())).getDateTimeString()%錛烇紲/td錛?br>錛渢d class="錛?= stl %錛?" nowrap=''錛?br>錛?= BeaconDate.getInstance( new java.util.Date(sess.getLastAccessedTime())).getDateTimeString()%錛烇紲/td錛?br>錛?tr錛?br>錛?
}
%錛?br>錛?table錛?br>錛?body錛?
銆銆浠ヤ笂鐨勪唬鐮佷粠application涓彇鍑篴ctiveSessions錛屽茍涓旀樉紺哄嚭鍏蜂綋鐨勬椂闂淬傚叾涓瑽eaconDate綾誨亣璁句負鏍煎紡鍖栨椂闂寸殑綾匯?/p>
銆銆榪欐牱錛屾垜浠緱鍒頒簡涓涓療鐪嬪湪綰跨敤鎴風殑鍒楄〃鐨勬鏋躲傝嚦浜庡湪綰跨敤鎴峰垪琛ㄥ垎欏電瓑鍔熻兘錛屼笌鏈枃鏃犲叧錛屼笉浜堣璁恒?/p>
銆銆榪欐槸涓涓潪鍒鋒柊妯″瀷鐨勪緥瀛愶紝渚濊禆浜巗ession鐨勮秴鏃舵満鍒躲傛垜鐨勫悓浜媠onymusic鎸囧嚭寰堝鏃跺欑敱浜庡悇涓巶鍟嗘濇兂鐨勪笉鍚岋紝榪欐湁鍙兘鏄笉鍙俊璧栫殑銆傝冭檻鍒拌繖縐嶉渶姹傦紝闇瑕佸湪姣忎釜鍙墮潰鍒鋒柊鐨勬椂鍊欓兘鍒ゆ柇褰撳墠鐢ㄦ埛璺濈涓婃浣跨敤鐨勬椂闂存槸鍚﹁秴榪囨煇涓涓瀹氭椂闂村箋傝繖瀹炶川涓婂氨鏄嚜宸卞疄鐜皊ession瓚呮椂銆傚鏋滈渶瑕佸疄鐜板埛鏂版ā鍨嬶紝灝卞繀欏諱嬌鐢ㄨ繖縐嶆瘡涓彾闈㈣繘琛屽埛鏂板垽鏂殑鏂規硶銆?/p>