一個賬號同一時間只能一我的登陸

對於一個賬號在同一時間只能一我的登陸,能夠經過下面的方法實現:javascript

1 .在用戶登陸時,把用戶添加到一個ArrayList中java

2 .再次登陸時查看ArrayList中有沒有該用戶,若是ArrayList中已經存在該用戶,則阻止其登陸ajax

3 .當用戶退出時,須要從該ArrayList中刪除該用戶,這又分爲三種狀況瀏覽器

① 使用註銷按鈕正常退出安全

② 點擊瀏覽器關閉按鈕或者用Alt+F4退出,能夠用javascript捕捉該頁面關閉事件,服務器

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

③ 非正常退出,好比客戶端系統崩潰或忽然死機,能夠採用隔一段時間session沒活動就刪除該session所對應的用戶來解決,這樣用戶須要等待一段時間以後就能夠正常登陸。jsp

在LoginAction中定義:ide

// 用來在服務器端存儲登陸的全部賬號url

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,並實現sessionCreated,sessionDestroyed方法,在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代碼就不須要了。

雖然 這樣作 也沒有問題 就怕 他不太穩定 不知道你們還 有沒有 更好的方法

相關文章
相關標籤/搜索