7th Chapter Listener Listener

Source: Internet
Author: User

Listener Overview

Use of listener

The corresponding listener interface needs to be implemented using listener.

public class Sessionlistenertest implements Httpsessionlistener {

public void sessioncreated (Httpsessionevent se) {

HttpSession session = Se.getsession ();

System.out.println (" newly created a session:" + Session);

}

public void sessiondestroyed (Httpsessionevent se) {

HttpSession session = Se.getsession ();

System.out.println (" destroyed a session:" + Session);

}

}

The above code implements the Httpsessionlistener interface. When you create a session, the server calls the Sessioncreated () method, and the server calls the Sessiondestroyed () method when the session is destroyed. They all take the Httpsessionevent object as a parameter and can get the session from this parameter. The listener needs to be configured in Web. XML to take effect.

The configuration is as follows:

<listener>

<listener-class>

Com.helloweenvsfei.listener.SessionListenerTest

</listener-class>

</listener>

Categories of listener

1. creation and destruction of listener objects

Httpsessionlistener,httpcontextlistener,httprequestlistener are used for the creation and destruction of Session,context,request respectively.

You can implement multiple listener interfaces in a class, such as the following code:

2. monitoring object's property changes

The code is as follows:

3. listen for objects in session

Httpsessionbindinglistener and Httpsessionactivationlistener

Httpsessionbindinglistener: Executes the Valuebound (Httpsessionbinding event) method when the object is placed in the session, Executes the Valueunbound (Httpsessionbinding event) method when the object is removed. The listener object must implement the interface.

Httpsessionactivationlistener: When the server shuts down, the contents of the session will be saved to the hard disk, this process is called passivation. When the server starts, the contents of the hard disk are loaded in. The Sessionwillpassivate (httpsessionevent event) is executed when the object in session is deactivated, and sessiondidactivate is executed when the object is loaded (httpsessionevent Event). The listener object must implement the interface.

Listener use case (Single sign-on)

public class Loginsessionlistener implements Httpsessionattributelistener {

Log log = Logfactory.getlog (This.getclass ());

map<string, httpsession> map = new hashmap<string, httpsession> ();

public void attributeadded (Httpsessionbindingevent event) {

String name = Event.getname ();

Login

if (Name.equals ("PersonInfo")) {

PersonInfo PersonInfo = (PersonInfo) event.getvalue ();

if (Map.get (Personinfo.getaccount ()) = null) {

There is a record in the map indicating that the account has been logged on to another machine, invalidating the previous login

HttpSession session = Map.get (Personinfo.getaccount ());

PersonInfo Oldpersoninfo = (PersonInfo) session

. getattribute ("PersonInfo");

Log.info ("account" + oldpersoninfo.getaccount () + "on"

+ oldpersoninfo.getip () + "already logged in, the login will be forced offline. ");

Session.removeattribute ("PersonInfo");

Session.setattribute ("msg", "Your account has been logged on to another machine, you are forced to downline.") ");

}

The session is indexed by user name and placed in a map

Map.put (Personinfo.getaccount (), event.getsession ());

Log.info ("account" + personinfo.getaccount () + "in" + Personinfo.getip ()

+ "Login. ");

}

}

public void Attributeremoved (Httpsessionbindingevent event) {

String name = Event.getname ();

Cancellation

if (Name.equals ("PersonInfo")) {

Remove the session from the map

PersonInfo PersonInfo = (PersonInfo) event.getvalue ();

Map.Remove (Personinfo.getaccount ());

Log.info ("account" + personinfo.getaccount () + "logout. ");

}

}

public void attributereplaced (Httpsessionbindingevent event) {

String name = Event.getname ();

Sign in with another account without logging off

if (Name.equals ("PersonInfo")) {

Remove old sign-in information

PersonInfo Oldpersoninfo = (PersonInfo) event.getvalue ();

Map.Remove (Oldpersoninfo.getaccount ());

New sign-in information

PersonInfo PersonInfo = (PersonInfo) event.getsession ()

. getattribute ("PersonInfo");

Also check to see if the newly signed-in account has logged on on another machine.

if (Map.get (Personinfo.getaccount ()) = null) {

There is a record in the map indicating that the account has been logged on to another machine, invalidating the previous login

HttpSession session = Map.get (Personinfo.getaccount ());

Session.removeattribute ("PersonInfo");

Session.setattribute ("msg", "Your account has been logged on to another machine, you are forced to downline.") ");

}

Map.put ("PersonInfo", Event.getsession ());

}

}

}

Code Explanation:

Each time a user logs in, the JavaBean Personinfo object is placed in the session, which triggers the attributeadded method in the listener Loginsessionlistener. This method obtains the account of the Personinfo and the information in the existing map set, see if there are duplicates, if there are duplicates, get the duplicate session, and remove the Personinfo information of the session.

HttpSession session = Map.get (Personinfo.getaccount ());

Session.removeattribute ("PersonInfo");

Session.setattribute ("msg", "Your account has been logged on to another machine, you are forced to downline.") ");

The reason why the above code can remove the original login information is as follows:

Parameter Passing in Java is a reference to a pass, so the session in the Map collection is a reference to the original session.

The code httpsession session = Map.get (Personinfo.getaccount ()), and the original session is obtained. So deleting the personinfo of the session is to delete the personinfo of the original session.

 

7th Chapter Listener Listener

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.