Java WEB Implementation QQ Login function An account can only log on one person at a time _java

Source: Internet
Author: User
Tags static class java web

For an account at the same time only one person login, you can do this in the following ways:

1. Add the user to a ArrayList when the user logs in

2. When you log on again, see if the user is in ArrayList, and if the user already exists in ArrayList, prevent it from logging in

3. When the user exits, the user needs to be removed from the ArrayList, which is divided into three different cases

① use logout button to exit normally

② Click the browser Close button or exit with ALT+F4, you can capture the page shutdown event with JavaScript,

Perform a section of Java method to delete a user in ArrayList

③ abnormal exit, such as the client system crashes or sudden panic, can be used for a period of time without activity to delete the session of the corresponding users to resolve, so that users need to wait a period of time after the normal login.

Defined in Loginaction:

All accounts that are used to store logins on the server side are public
static List logonaccounts;

Login () Login method:

Set session inactivity time to 30 minutes
request.getsession (). Setmaxinactiveinterval (60*30);
if (logonaccounts==null) {
logonaccounts = new ArrayList ();
}
See if ArrayList has this user for
(int i = 0; i < logonaccounts.size (); i++) {account
Existaccount = (account) LOGONACC Ounts.get (i);
if (Account.getaccountid (). Equals (Existaccount.getaccountid ()) {return
"denied";
}
}
When the user logs on, add SessionID to a Account object
//In the back ③ need to delete the corresponding user Account.setsessionid according to this SessionID
( Request.getsession (). GetId ());
The user is saved to the ArrayList static class variable in
logonaccounts.add (account);
return "Login";

① use logout button to exit normally

Logout () Exit Method:

if (logonaccounts==null) {
logonaccounts = new ArrayList ();
}
Delete the user in ArrayList ⑴ for
(int i = 0; i < logonaccounts.size (); i++) {account
Existaccount = (account) Logonac Counts.get (i);
if (Account.getaccountid (). Equals (Existaccount.getaccountid ())) {
logonaccounts.remove (account);
}
}

② Click the browser Close button or exit with ALT+F4:

Pop up a window in the background and remove the user from the ArrayList in the pop-up window

function Window.onbeforeunload () {//
whether by closing the button or using Alt+f4
to exit///If the onbeforeunload event is triggered for refreshing, the following if statement does not execute
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: Remove users from ArrayList in pop-up window

<% account [Account
] request.getsession (). getattribute ("account");
if (account!= null) {
if (loginaction.logonaccounts==null) {
loginaction.logonaccounts = new ArrayList ();
}
//Delete the user in ArrayList--the following code is the same as the ⑴ at the top
(int i = 0; i < logonaccounts.size (); i++) {account
Existaccoun T = (account) logonaccounts.get (i);
if (Account.getaccountid (). Equals (Existaccount.getaccountid ())) {
logonaccounts.remove (account)}}}
%>

To ensure that the above code is complete, close this pop-up window (also in accountunbound.jsp) after 3 seconds.

<script>
settimeout ("CloseWindow ();", 3000);
function CloseWindow () {
window.close ();
}
</script>

③ enables Loginaction to implement implements Httpsessionlistener, and implements the Sessioncreated,sessiondestroyed method, Delete the user in the ArrayList in Sessiondestroyed (this method is performed if the user is inactive for more than 30 minutes)

public void sessiondestroyed (Httpsessionevent event) {
//Get inactive SessionID and delete user String in corresponding logonaccounts according to it
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)}}}

Note:

For the above, because pop-up windows are easily blocked by firewalls or security software, resulting in the inability to pop-up windows, so that a short time can not log in, this situation can use AJAX instead of pop-up windows, the same in the background to delete the user's code, but will not be subject to firewall restrictions:

 <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_re
Quest.overridemimetype (' Text/xml '); ' Else if ' window. ActiveXObject) {//IE try {http_request = new ActiveXObject ("Msxml2.xmlhttp");} catch (e) {try {http_request = new Ac
Tivexobject ("Microsoft.XMLHTTP"); The 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 =) {Window.close ();} else
{alert (' There is a problem with the request. ');} } function window. onBeforeUnload () {makerequest (' accountunbound.jsp ');}//]]> </script> 

For the above Ajax code, there are many detailed explanations on the Web, add it to the onbeforeunload () browser shutdown event, in the background to execute the code is very good, do not have to worry about pop-up windows sometimes ineffective problems.

Using this code, the above ② in the accountunbound.jsp in the pop-up window Window.close (), the JS code does not need.

The above is a small set to introduce the Java WEB implementation of QQ login function an account at the same time can only be a person login, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.