For an account that can only log in one person at the same time, it can be achieved by the following method:
1. Add the user to an ArrayList when the user logs in
2. When logging in again, check if there is any user in ArrayList. If the user already exists in ArrayList, it will be blocked from logging in.
3. When the user exits, the user needs to be deleted from the ArrayList, which is divided into three situations
① 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 a Java method to delete users in ArrayList
③ Unusual logout, such as a crash or sudden crash on the client system, you can use the user corresponding to the session to delete the session if the session is not active every once in a while, so that the user needs to wait for a while before logging in normally.
Defined in LoginAction:
// Used to store all accounts logged in on the server side public static List logonAccounts;
login() login method:
// Set the session inactivity time to 30 minutes request.getSession().setMaxInactiveInterval(60*30);if(logonAccounts==null){logonAccounts = new ArrayList();}// Check if there is any user in ArrayList 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, add sessionId to an account object// Later ③ You need to delete the corresponding user account.setSessionId(request.getSession().getId());// This user is saved to the ArrayList static class variable logonAccounts.add(account); return "login";① Use the 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)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, deletes the user in the ArrayList in the pop-up window
function window.onbeforeunload(){// Whether to exit through the close button or use Alt+F4// If the onbeforeunload event is triggered for refresh, 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: Delete the user in ArrayList in the pop-up window
<%Account 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 in ⑴ above for (int i = 0; i < logonAccounts.size(); i++) {Account existAccount = (Account)logonAccounts.get(i);if(account.getAccountId().equals(existAccount.getAccountId())){logonAccounts.remove(account);}}}%>In order to ensure that the above code can be executed, close this pop-up window after 3 seconds (also located in accountUnbound.jsp)
<script>setTimeout("closeWindow();",3000);function closeWindow(){window.close();}</script>③ Make LoginAction implements HttpSessionListener, and implement sessionCreated and sessionDestroyed methods. Delete users in ArrayList in sessionDestroyed (if the user is inactive for more than 30 minutes, this method will be executed)
public void sessionDestroyed(HttpSessionEvent event) {// Get the sessionId when inactive, and delete the user in the corresponding 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);}}}Note:
For the above, since the pop-up window is easily blocked by the firewall or security software, the pop-up window cannot be popped up, and therefore the short-term login cannot be logged in. In this case, AJAX can be used instead of the pop-up window. The code that deletes the user in the background is executed, but 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) { // IEtry {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> There are many detailed explanations for the above ajax code on the Internet. Adding it to the onbeforeunload() browser close event will work very well in the background, so there is no need to worry about the problem that pop-ups will sometimes be invalid.
After using this code, the js code in the accountUnbound.jsp in the above ② closes the pop-up window window.close(); is no longer needed.
The above is the Java Web implementation of QQ login function that the editor introduces to you. One account can only log in one person at the same time. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!