Java Web 實(shí)現(xiàn)QQ登錄功能一個(gè)帳號(hào)同一時(shí)間只能一個(gè)人登錄
對(duì)于一個(gè)帳號(hào)在同一時(shí)間只能一個(gè)人登錄,可以通過下面的方法實(shí)現(xiàn):
1 .在用戶登錄時(shí),把用戶添加到一個(gè)ArrayList中
2 .再次登錄時(shí)查看ArrayList中有沒有該用戶,如果ArrayList中已經(jīng)存在該用戶,則阻止其登錄
3 .當(dāng)用戶退出時(shí),需要從該ArrayList中刪除該用戶,這又分為三種情況
① 使用注銷按鈕正常退出
② 點(diǎn)擊瀏覽器關(guān)閉按鈕或者用Alt+F4退出,可以用JavaScript捕捉該頁(yè)面關(guān)閉事件,
執(zhí)行一段Java方法刪除ArrayList中的用戶
③ 非正常退出,比如客戶端系統(tǒng)崩潰或突然死機(jī),可以采用隔一段時(shí)間session沒活動(dòng)就刪除該session所對(duì)應(yīng)的用戶來解決,這樣用戶需要等待一段時(shí)間之后就可以正常登錄。
在LoginAction中定義:
// 用來在服務(wù)器端存儲(chǔ)登錄的所有帳號(hào) public static List logonAccounts;
login() 登錄方法中:
// 設(shè)置session不活動(dòng)時(shí)間為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";
}
}
// 在用戶登錄時(shí),把sessionId添加到一個(gè)account對(duì)象中
// 在后面 ③ 需要根據(jù)此sessionId刪除相應(yīng)用戶
account.setSessionId(request.getSession().getId());
// 該用戶保存到ArrayList靜態(tài)類變量中
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);
}
}
② 點(diǎn)擊瀏覽器關(guān)閉按鈕或者用Alt+F4退出:
在后臺(tái)彈出一個(gè)窗口,在彈出窗口中刪除ArrayList中的用戶
function window.onbeforeunload(){
// 是否通過關(guān)閉按鈕或者用Alt+F4退出
// 如果為刷新觸發(fā)onbeforeunload事件,下面if語(yǔ)句不執(zhí)行
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);
}
}
}
%>
為了保證上面代碼可以執(zhí)行完畢,3秒后關(guān)閉此彈出窗口(也位于accountUnbound.jsp中)
<script>
setTimeout("closeWindow();",3000);
function closeWindow(){
window.close();
}
</script>
③ 使LoginAction 實(shí)現(xiàn)implements HttpSessionListener,并實(shí)現(xiàn)sessionCreated,sessionDestroyed方法,在sessionDestroyed中刪除ArrayList中的用戶(用戶超過30分鐘不活動(dòng)則執(zhí)行此方法)
public void sessionDestroyed(HttpSessionEvent event) {
// 取得不活動(dòng)時(shí)的sessionId,并根據(jù)其刪除相應(yīng)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);
}
}
}
注:
對(duì)于上面的,由于彈出窗口很容易被防火墻或者安全軟件阻攔,造成無法彈出窗口,從而短時(shí)間不能登錄,這種情況可以用AJAX來代替彈出窗口,同樣在后臺(tái)執(zhí)行刪除用戶的那段代碼,卻不會(huì)受到防火墻限制:
<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>
對(duì)于上面的這段ajax代碼,在網(wǎng)上有很多詳細(xì)的解釋,把它加到onbeforeunload()瀏覽器關(guān)閉事件中,在后臺(tái)執(zhí)行代碼的效果很好,不必?fù)?dān)心彈出窗口有時(shí)候會(huì)無效的問題。
使用這段代碼后,上面②中accountUnbound.jsp中的那段關(guān)閉彈出窗口window.close();的js代碼就不需要了。
以上所述是小編給大家介紹的Java Web 實(shí)現(xiàn)QQ登錄功能一個(gè)帳號(hào)同一時(shí)間只能一個(gè)人登錄,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Java模擬實(shí)現(xiàn)QQ三方登錄(單點(diǎn)登錄2.0)
- java模仿實(shí)現(xiàn)QQ登錄界面
- Java實(shí)現(xiàn)QQ第三方登錄的示例代碼
- Java Swing仿QQ登錄界面效果
- java實(shí)現(xiàn)簡(jiǎn)單QQ登錄界面
- java代碼塊之簡(jiǎn)易qq登錄界面及按鈕顏色設(shè)置代碼
- JavaWeb實(shí)現(xiàn)同一帳號(hào)同一時(shí)間只能一個(gè)地點(diǎn)登陸(類似QQ登錄的功能)
- java實(shí)現(xiàn) 微博登錄、微信登錄、qq登錄實(shí)現(xiàn)代碼
- 使用java swing實(shí)現(xiàn)qq登錄界面示例分享
- Java?Swing實(shí)現(xiàn)QQ登錄頁(yè)面
相關(guān)文章
MyBatis實(shí)現(xiàn)物理分頁(yè)的實(shí)例
這篇文章主要介紹了MyBatis實(shí)現(xiàn)物理分頁(yè)的實(shí)例,MyBatis使用RowBounds實(shí)現(xiàn)的分頁(yè)是邏輯分頁(yè),有興趣的可以了解一下。2017-01-01
Java CAS底層實(shí)現(xiàn)原理實(shí)例詳解
這篇文章主要介紹了Java CAS底層實(shí)現(xiàn)原理實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
java中的阻塞隊(duì)列應(yīng)用場(chǎng)景及代碼實(shí)例
這篇文章主要介紹了java中的阻塞隊(duì)列應(yīng)用場(chǎng)景及代碼實(shí)例阻塞隊(duì)列是一種特殊的隊(duì)列,它提供了線程安全的操作,并在隊(duì)列為空或滿時(shí)提供了阻塞的功能,阻塞隊(duì)列通常用于多線程場(chǎng)景,其中生產(chǎn)者線程向隊(duì)列中添加元素,而消費(fèi)者線程從隊(duì)列中獲取元素,需要的朋友可以參考下2024-01-01
Spring框架實(shí)現(xiàn)AOP的兩種方式詳解
這篇文章主要為大家詳細(xì)介紹了Spring框架實(shí)現(xiàn)AOP的兩種方式,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的借鑒價(jià)值,需要的可以參考一下2022-09-09
@PostConstruct在項(xiàng)目啟動(dòng)時(shí)被執(zhí)行兩次或多次的原因及分析
這篇文章主要介紹了@PostConstruct在項(xiàng)目啟動(dòng)時(shí)被執(zhí)行兩次或多次的原因及分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

