Source: http://blog.csdn.net/mydream83/archive/2007/02/18/1511639.aspx
I have been in the company for two months, and I have been in the group for more than one month. This is my first job and the first project. So I decided to keep a text record, because the group was relatively late, I am also relatively independent in charge of system management. The first thing to do is to log on to the system. Since the application system certainly does not allow the same account to log on repeatedly in different regions, I have checked a lot of information on this issue, I want to do something perfect, but I have heard a hundred words.
The methods I currently know can be divided into two types. One is to record the online status of the user in the database, assign values to the ID field of the user database during login, and reset the field when exiting. the second is to record the user's situation through the global memory object, and store the user's login name, such as the user, as the key in the application or a global hashmap defined, the information corresponding to the user in the global object is deleted. It is said that the global object can be cached,. net, JAVA Implementation of cache is not too easy. these can be used to handle user login events in real time, but there are obviously a variety of user exits. The possibility of clicking Cancel is very small, so you have to consider power outages, disconnection, and other situations, so the user exits. Program It cannot be captured. This is determined by the session mechanism. Therefore, when a user exits, it is almost impossible for the server to perform real-time refresh (unless the user logs out ), many online statistics are not real-time. Generally, the session listener is added and the session expiration time is set. When the time is up, the corresponding method of the listener is called to release relevant data, for example, delete the user information in the application. therefore, it is very likely that the user just logged on to the system, then dropped the line or another reason, and then logged on again, but the system prompts that the user has logged on and cannot log on again. you can only wait until the original session expires. In this case, it must be very depressing. this problem cannot be solved. The root cause is that the user's exit is uncertain, and the server cannot distinguish whether the user is online or false. in this way, the top number can be processed like QQ, that is, when a user logs on to the system, it determines whether the user information exists in the application. If yes, It is replaced. If no, it is written, in this way, the first logon will be After logging out, you do not need to worry about whether the user logged in first is real online or false online, achieving real-time results. However, our project won't be used in this way, so we can only use session listening. There are eight listeners in the servlet, four of which are the two session events associated with the session listening interface,
These four interfaces are:
Httpsessionattributelistener and httpsessionbindingevent
Httpsessionbindinglistener and httpsessionbindingevent
Httpsessionlistener and httpsessionevent
Httpsessionactivationlistener and httpsessionevent
Httpsessionattributelistener: Used to listen for adding, replacing, and deleting attributes in the session. You only need to create a class to implement the httpsessionattributelistener interface, and then click. XML is configured with a listener attribute, where litener-class is the path of this class. this class implements three methods of httpsessionattributelistener. add, replace, and delete corresponding properties in the session. this class does not need to be instantiated, because the servlet container will create an instance of this class and is a singleton. therefore, all user operations on the session attribute accessed by the system will be monitored by httpsessionattributelistener. the trigger object of the httpsessionbindingevent event is an instance of any object, because the setattribute method of the session can store instances of any object. Httpsessionbindinglistener: Creates a class that implements the httpsessionbindinglistener interface and does not need to be stored on the web. configured in XML. This class implements the valuebound () and valueunbound () Methods of httpsessionbindinglistener. This class needs to be instantiated and used. The valuebound () method will be triggered immediately after the instance object is put into the session, remove the instance from the session or the Session Timeout will immediately trigger valueunbound (). Obviously, each servlet creates an object of this class, so they are independent of each other during concurrent calls, in addition, you can create multiple classes that implement the httpsessionbindinglistener interface, with the same effect.
The trigger object of the httpsessionbindingevent event is the instance itself (a bit detour ),
Example
Public class sessionlistener implements httpsessionbindinglistener ....
Sessionlistener = new sessionlistener ();
// The following sentence triggers the httpsessionbindingevent event. The trigger object is sessionlistener, And the listener instance itself.
Request. getsession (). setattribute (request. getsession (). GETID (), sessionlistener );
Httpsessionlistener: listens for the creation and release of sessions, which must be performed on the web. configure in XML and override sessioncreated () and sessiondestroyed () methods. It is also a single-column object created by the container and can listen to sessions created by all users. This method is suitable for statistics, note that the session is created when the browser accesses the server, and the sessiondestroyed () method is called only after the session is released. Therefore, the sessiondestroyed () method cannot be used in sessiondestroyed () to clear the session.
Httpsessionactivationlistener: when the session is distributed across JVM, the object implementing this interface is notified. The methods sessiondidactivate () and sessionwillpassivate () are required (), activate and passivate are actions used to replace objects. When session objects must be temporarily stored on hard disks or other storage devices for reasons such as resource utilization or load balancing (serialized through objects ), the action is called passivate, and the action taken when the session object on the hard disk or storage device reloads the JVM is called Activate. It is easy to understand that sessiondidactivate () and sessionwillpassivate () call after activeate and before passivate respectively.
These four parameters are related to the session, but only httpsessionbindinglistener and httpsessionlistener can be used to verify logon.