Java Web implementation of QQ login function an account can only be logged in by one person at a time

  • 2020-05-17 05:23:33
  • OfStack

For one account, only one person can log in at the same time, the following methods can be used:

1. When the user logs in, add the user to an ArrayList

2. Check if the user is in ArrayList when you log in again, and if the user is already in ArrayList, block the user from logging in

3. When a user exits, the user needs to be deleted from the ArrayList, which can be divided into three scenarios

Use the logout button to exit normally

Click the browser close button or use Alt+F4 to exit. You can use JavaScript to capture the page close event.

Execute section 1 of the Java method to delete the user in ArrayList

(3) abnormal exit, such as client system crash or sudden crash, can be solved by deleting the corresponding user of session after 1 period of no activity, so that the user needs to wait for 1 period of time before normal login.

Defined in LoginAction:


//  Use to store all logon accounts on the server side 
public static List logonAccounts;

In the login() login method:


//  Set up the session The inactive time is 30 points 
request.getSession().setMaxInactiveInterval(60*30);
if(logonAccounts==null){
logonAccounts = new ArrayList();
}
//  To view ArrayList There is no such user in China 
for (int i = 0; i < logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
return "denied";
}
}
//  When the user logs in, the sessionId Added to the 1 a account In the object 
//  In the back   3.   According to this sessionId Delete the corresponding user 
account.setSessionId(request.getSession().getId());
//  The user saves to ArrayList In a static class variable 
logonAccounts.add(account);
return "login";

Use the logout button to exit normally

In the logout() exit method:


if(logonAccounts==null){
logonAccounts = new ArrayList();
}
//  delete ArrayList The user   (1) 
for (int i = 0; i < logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
logonAccounts.remove(account);
}
}

Click the browser close button or use Alt+F4 to exit:

A window pops up in the background to delete the user in ArrayList


function window.onbeforeunload(){
//  Whether through the close button or with Alt+F4 exit 
//  If triggered for a refresh onbeforeunload Events, the following if Statement not executed 
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: delete the user in ArrayList from the pop-up window


<%
Account account = (Account) request.getSession().getAttribute("account");
if(account != null){
if(LoginAction.logonAccounts==null){
LoginAction.logonAccounts = new ArrayList();
}
//  delete ArrayList The user -- the following code and the above   (1)   place 1 sample 
for (int i = 0; i < logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
logonAccounts.remove(account);
}
}
}
%>

To ensure that the above code can be executed, close this pop-up window (also in accountUnbound.jsp) after 3 seconds


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

Make LoginAction implement implements HttpSessionListener, and implement sessionCreated, sessionDestroyed method to delete the user in ArrayList in sessionDestroyed (execute this method if the user is inactive for more than 30 minutes)


public void sessionDestroyed(HttpSessionEvent event) {
//  Gets when inactive sessionId, And delete accordingly logonAccounts The user 
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);
}
}
}

Note:

As for the above, because the popup window is easily blocked by firewall or security software, it cannot be popup window, so it cannot be logged in for a short time. In this case, AJAX can be used to replace the popup window. Also, the user's code is deleted in the background, but it will not be restricted by the firewall:


<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>

The above ajax code has been explained in great detail on the web, but it works well to add it to the onbeforeunload() browser shutdown event and execute the code in the background without worrying about the pop-up window sometimes being invalid.

After using this code, the section in accountUnbound.jsp above closes the pop-up window window.close (); The js code is not needed.


Related articles: