In jsp web, only one location can be logged on to the same account at a time

Source: Internet
Author: User
Tags sessions
In jsp web, the same account can only log on to one location at a time. 1. What is the function for you to think about. There will always be such a demand. There will be no demand in this year .. Haha. Sometimes it is not necessarily a requirement. It is very likely that this will also be done for security. Such as the examination system and online chat system. II. Implementation process a. Problem analysis in the system, we usually bind the login information to the session. From this perspective, we may find a solution. To put it bluntly, when the user logs on, it determines whether the user has logged on. If the user logs on, it is OK to clear the previous session .. Does it seem simple? As a matter of fact, you will find the following problems: how can you obtain the information about the previous user's logon, that is, how to access all the logged-on sessions? B. As you can see, there seems to be no specific method in j2ee api to directly obtain all session information. However, you can configure listeners to monitor the creation, deletion, and replacement of all sessions, as well as the creation, deletion, and replacement of attributes in sessions. In fact, we only need to do the following: when saving the user login information to the session, it corresponds to the creation process of a session attribute (attributeAdded ), you can record the current session to an ArrayList. In fact, when saving to the list, you must first traverse whether the user's logon information already exists in the list. If yes, the session information in the list will be destroyed and removed from the list. If no session information exists, the session information will be placed in the list. When the session login information is destroyed, the sesseion is directly removed from the list. In addition, when a user does not log on and does not log on directly, this is a session attribute replacement process. It also needs to be processed to determine whether a new user exists in other sessions except the current session. If yes, delete it. The code is as follows: package com. weirhp; import java. util. arrayList; import java. util. collections; import java. util. list; import javax. servlet. http. httpSession; import javax. servlet. http. httpSessionAttributeListener; import javax. servlet. http. httpSessionBindingEvent; import javax. servlet. http. httpSessionEvent; import javax. servlet. http. httpSessionListener; public class RecordSessionListener implements HttpSessionAttributeListener, HttpSessionListener {private static ListSessions; public static String loginFlag = "loginUser"; static {if (sessions = null) {sessions = Collections. synchronizedList (new ArrayList() ;}} Public void attributeAdded (HttpSessionBindingEvent e) {HttpSession session = e. getSession (); System. out. println ("------------- * start added * ---------------------"); String attrName = e. getName (); // log on to if (attrName. equals (loginFlag) {User nowUser = (User) e. getValue (); User sUser = (User) session. getAttribute (loginFlag); // traverses all sessions for (int I = sessions. size ()-1; I> = 0; I --) {Sess IonAndUser tem = sessions. get (I); if (tem. getUserID (). equals (nowUser. getName () {tem. getSession (). invalidate (); // automatically call remove break;} SessionAndUser sau = new SessionAndUser (); sau. setUserID (nowUser. getName (); sau. setSession (session); sau. setSid (session. getId (); sessions. add (sau) ;}} public void attributeRemoved (HttpSessionBindingEvent e) {HttpSession session = e. getSession (); System. out. pr Intln ("------------- * start Removed * -----------------------"); String attrName = e. getName (); // log on to if (attrName. equals (loginFlag) {User nowUser = (User) e. getValue (); // traverses all sessions for (int I = sessions. size ()-1; I> = 0; I --) {SessionAndUser tem = sessions. get (I); if (tem. getUserID (). equals (nowUser. getName () {sessions. remove (I); break ;}}} public void attributeReplaced (HttpSessionBindin GEvent e) {HttpSession session = e. getSession (); System. out. println ("------------- * start replace * ---------------------"); String attrName = e. getName (); int delS =-1; // log on to if (attrName. equals (loginFlag) {// User nowUser = (User) e. getValue (); // old value User nowUser = (User) session. getAttribute (loginFlag); // The user in the current session // traverses all sessions for (int I = sessions. size ()-1; I> = 0; I --) {SessionA NdUser tem = sessions. get (I); if (tem. getUserID (). equals (nowUser. getName ())&&! Tem. getSid (). equals (session. getId () {System. out. println ("Remove: invalidate 1! "); DelS = I;} else if (tem. getSid (). equals (session. getId () {tem. setUserID (nowUser. getName () ;}} if (delS! =-1) {sessions. get (delS). getSession (). invalidate (); // The remove method is automatically called when it becomes invalid. It will also be removed from sessions} public void sessionCreated (HttpSessionEvent e) {} public void sessionDestroyed (HttpSessionEvent e) {} in the web. xml preparation 12 RecordSession3 Com. weirhp. RecordSessionListener4
Related Article

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.