The application scope implements user login to squeeze out the previously logged in user code
1、 Realization thought
1. Application (ServletContext) is a scope saved on the server side. We save two forms of key value pairs in application: 1: < userid, sessionid >, 2: < sessionid, session >
2. Whenever a user logs in (a new session will be generated), first query the sessionid in the application according to the userid:
If sessionid is not queried, it means that no user has logged in to this account, then save < userid, sessionid > and < sessionid, session > data to the application
If the sessionid is queried, it indicates that a user has logged in. The following three steps will be performed:
1) get the logged in session first and invalidate it
2) delete the original session from the application and save the new session to the application (< sessionid, session >)
3) finally, delete the original sessionid from the application and save the new sessionid to the application (< userid, sessionid >)
2、 The code of the handler that implements the login function
3、 Summary
1. Please note why < userid, sessionid > is specially used to save sessionid?
When the second user logs in, we must get the sessionid of the first user if we want to invalidate the session of the first user. Therefore, we need to save the sessionid in the form of < userid, sessionid >, so that we can find the sessionid of the first user through the userid, so as to find the session of the first user and invalidate it
The above is all about the implementation of user login in the application scope in this paper, which squeezes out the previous login user code. I hope it will be helpful to you. Interested friends can continue to refer to this site: java thread lock object lock synchronization problem, more perfect processing method code examples, sharing a simple java crawler framework, list usage code examples in Java collection, etc. if you have any questions, you can leave a message at any time, and the editor will reply to you in time. Thank you for your support!
