<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 23,comments - 15,trackbacks - 0

    對于一個帳號在同一時間只能一個人登錄,可以通過下面的方法實現:

    1 .在用戶登錄時,把用戶添加到一個ArrayList

    2 .再次登錄時查看ArrayList中有沒有該用戶,如果ArrayList中已經存在該用戶,則阻止其登錄

    3 .當用戶退出時,需要從該ArrayList中刪除該用戶,這又分為三種情況

    使用注銷按鈕正常退出

    點擊瀏覽器關閉按鈕或者用Alt+F4退出,可以用javascript捕捉該頁面關閉事件,

    執行一段java方法刪除ArrayList中的用戶

    非正常退出,比如客戶端系統崩潰或突然死機,可以采用隔一段時間session沒活動就刪除該session所對應的用戶來解決,這樣用戶需要等待一段時間之后就可以正常登錄。

     

    LoginAction中定義:

    // 用來在服務器端存儲登錄的所有帳號

    public static List logonAccounts;

     

    login() 登錄方法中:

    // 設置session不活動時間為30

    request.getSession().setMaxInactiveInterval(60*30);

    if(logonAccounts==null){

        logonAccounts = new ArrayList();

    }

    // 查看ArrayList中有沒有該用戶

    for (int i = 0; i < logonAccounts.size(); i++) {

        Account existAccount = (Account)logonAccounts.get(i);

        if(account.getAccountId().equals(existAccount.getAccountId())){

            return "denied";

        }

    }

    // 在用戶登錄時,把sessionId添加到一個account對象中

    // 在后面 需要根據此sessionId刪除相應用戶

    account.setSessionId(request.getSession().getId());

    // 該用戶保存到ArrayList靜態類變量中

    logonAccounts.add(account);

    return "login";

     

    使用注銷按鈕正常退出

    logout() 退出方法中:

    if(logonAccounts==null){

        logonAccounts = new ArrayList();

    }

    // 刪除ArrayList中的用戶  ⑴

    for (int i = 0; i < logonAccounts.size(); i++) {

        Account existAccount = (Account)logonAccounts.get(i);

        if(account.getAccountId().equals(existAccount.getAccountId())){

            logonAccounts.remove(account);

        }

    }

     

    點擊瀏覽器關閉按鈕或者用Alt+F4退出:

    在后臺彈出一個窗口,在彈出窗口中刪除ArrayList中的用戶

    function window.onbeforeunload(){

    // 是否通過關閉按鈕或者用Alt+F4退出

    // 如果為刷新觸發onbeforeunload事件,下面if語句不執行

        if (event.clientX>document.body.clientWidth && event.clientY<0||event.altKey){

            window.open('accountUnbound.jsp','',

                    'height=0,width=0,top=10000,left=10000')

        }

    }

     

     

    accountUnbound.jsp : 彈出窗口中刪除ArrayList中的用戶

    <%

        Account account = (Account) request.getSession().getAttribute("account");

        if(account != null){

            if(LoginAction.logonAccounts==null){

                LoginAction.logonAccounts = new ArrayList();

            }

            // 刪除ArrayList中的用戶——下面代碼和上面的 處一樣

            for (int i = 0; i < logonAccounts.size(); i++) {

                Account existAccount = (Account)logonAccounts.get(i);

                if(account.getAccountId().equals(existAccount.getAccountId())){

                    logonAccounts.remove(account);

                }

            }

        }

    %>

    為了保證上面代碼可以執行完畢,3秒后關閉此彈出窗口(也位于accountUnbound.jsp中)

    <script>

    setTimeout("closeWindow();",3000);

    function closeWindow(){

        window.close();

    }

    </script>

     

    使LoginAction 實現implements HttpSessionListener,并實現sessionCreatedsessionDestroyed方法,在sessionDestroyed中刪除ArrayList中的用戶(用戶超過30分鐘不活動則執行此方法)

    public void sessionDestroyed(HttpSessionEvent event) {

       // 取得不活動時的sessionId,并根據其刪除相應logonAccounts中的用戶

       String sessionId = event.getSession().getId();

       for (int i = 0; i < logonAccounts.size(); i++) {

           Account existAccount = (Account)logonAccounts.get(i);

           if(account.getSessionId().equals(existAccount.getSessionId())){

               logonAccounts.remove(account);

           }

       }

    }

     

    注:

    對于上面的,由于彈出窗口很容易被防火墻或者安全軟件阻攔,造成無法彈出窗口,從而短時間不能登錄,這種情況可以用AJAX來代替彈出窗口,同樣在后臺執行刪除用戶的那段代碼,卻不會受到防火墻限制:

    <script>

        // <![CDATA[

        var http_request = false;

        function makeRequest(url) {

            http_request = false;

            if (window.XMLHttpRequest) { // Mozilla, Safari,...

                http_request = new XMLHttpRequest();

                if (http_request.overrideMimeType) {

                    http_request.overrideMimeType('text/xml');

                }

            } else if (window.ActiveXObject) { // IE

                try {

                    http_request = new ActiveXObject("Msxml2.XMLHTTP");

                } catch (e) {

                    try {

                        http_request = new ActiveXObject("Microsoft.XMLHTTP");

                    } catch (e) {

                    }

                }

            }

            if (!http_request) {

                alert('Giving up :( Cannot create an XMLHTTP instance');

                return false;

            }

            http_request.onreadystatechange = alertContents;

            http_request.open('GET', url, true);

            http_request.send(null);

     

        }

        function alertContents() {

            if (http_request.readyState == 4) {

                if (http_request.status == 200) {

                    window.close();

                } else {

                    alert('There was a problem with the request.');

                }

            }

     

        }

        function window. onbeforeunload() {

            makeRequest ('accountUnbound.jsp');

        }

        //]]>

    </script>

     

    對于上面的這段ajax代碼,在網上有很多詳細的解釋,把它加到onbeforeunload()瀏覽器關閉事件中,在后臺執行代碼的效果很好,不必擔心彈出窗口有時候會無效的問題

     

    使用這段代碼后,上面accountUnbound.jsp中的那段關閉彈出窗口window.close();js代碼就不需要了。

    posted on 2007-06-19 13:12 飛翔的心 閱讀(186) 評論(0)  編輯  收藏 所屬分類: Java

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 毛片网站免费在线观看| 日本最新免费网站| 四虎国产精品免费视| 亚洲日本人成中文字幕| 很黄很色很刺激的视频免费| 亚洲欧洲另类春色校园小说| 1000部拍拍拍18勿入免费视频软件 | 99re8这里有精品热视频免费| 中文字幕亚洲天堂| 国产免费久久久久久无码| 亚洲人成人77777网站| 成人影片一区免费观看| 亚洲精品永久www忘忧草| 国产92成人精品视频免费| 亚洲精品不卡视频| 丁香花在线观看免费观看| 无码天堂亚洲国产AV| 久久久久亚洲AV综合波多野结衣| 中国一级特黄的片子免费 | 免费福利电影在线观看| 久久久亚洲AV波多野结衣| 欧洲黑大粗无码免费| 亚洲AV无码成人精品区狼人影院| 亚洲第一区精品观看| 国产一区二区三区免费| 亚洲乱人伦精品图片| 免费**毛片在线播放直播| 女人隐私秘视频黄www免费| 亚洲系列中文字幕| 日韩高清在线免费看| 9久久免费国产精品特黄| 亚洲日韩乱码中文无码蜜桃臀| 国产成人aaa在线视频免费观看 | 18禁美女黄网站色大片免费观看 | 国产亚洲欧洲精品| 精品国产sm捆绑最大网免费站| 亚洲中文字幕久久无码| 综合亚洲伊人午夜网 | 在线播放免费人成视频网站| 亚洲丁香色婷婷综合欲色啪| 日韩高清免费观看|