Java Web realizes QQ login function. One account can only be logged in by one person at the same time
For an account, only one person can log in at the same time, which can be realized by the following methods:
1. When the user logs in, add the user to an ArrayList
2. Check whether the user exists in the ArrayList when logging in again. If the user already exists in the 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 cases
① Use the logout button to exit normally
② Click the browser close button or exit with ALT + F4 to capture the page close event with JavaScript,
Execute a Java method to delete users in ArrayList
③ Abnormal exit, such as client system crash or sudden crash, can be solved by deleting the user corresponding to the session after a period of time when the session is inactive, so that the user can log in normally after waiting for a period of time.
Define in loginaction:
In the login() login method:
① Use the logout button to exit normally
Logout() exit method:
② Click the browser close button or exit with ALT + F4:
A window pops up in the background, in which users in ArrayList are deleted
accountUnbound. Jsp: delete the user in the ArrayList in the pop-up window
To ensure that the above code can be executed, close the pop-up window (also in accountunbound. JSP) after 3 seconds
③ Make loginaction implement implements httpsessionlister, and implement sessioncreated and sessiondestroyed methods. Delete the user in ArrayList in sessiondestroyed (execute this method if the user is inactive for more than 30 minutes)
Note:
For the above, because the pop-up window is easily blocked by the firewall or security software, the pop-up window cannot be popped up, so the login cannot be performed for a short time. In this case, AJAX can be used to replace the pop-up window. Similarly, the code to delete the user in the background will not be limited by the firewall:
For the above Ajax code, there are many detailed explanations on the Internet. Add it to the onbeforeunload () browser closing event. The effect of executing the code in the background is very good. There is no need to worry about the invalidity of pop-up windows sometimes.
After using this code, accountunbound The section in JSP closes the pop-up window window close(); JS code is not needed.
The above is what Xiaobian introduced to you. Java Web realizes QQ login function. One account can only be logged in by one person at a time. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!
