Httpsessionbindinglistener Implementation and Application

Source: Internet
Author: User

The httpsessionbindinglistener interface has two methods to implement:
Public synchronized void valuebound (httpsessionbindingevent)
Public synchronized void valueunbound (httpsessionbindingevent)

When a session is created, the servlet container will call the valuebound method. When a session is deleted, the valueunbound method is called.

A specific application can track every logged-on user, record relevant information, and promptly Delete relevant information when it times out.

The instance code is as follows:
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;
....
....
Public synchronized void valuebound (httpsessionbindingevent ){
// Check whether it is valid
If (! Valid () return;
// Check whether the information already exists. If the information does not exist, add it.

If (info. Online. containskey (m_userid ))
{
// Record log information
Log.info ("initial user context ....");
Useronlinestruct UOS = (useronlinestruct) info. Online. Get (m_userid );
UOS. loginip = UOS. loginip;
UOS. logintime = UOS. logintime;
UOS. sessionid = UOS. sessionid;
}
Else {
Info. Online. Put (m_userid, UOS );
}
}

Public synchronized void valueunbound (httpsessionbindingevent ){
// Check whether it is valid
If (! Valid () return;
// Delete user information
// Record log information
Log.info ("destory user context ....");
Info. Online. Remove (m_userid );
}

--------------------------------
If you do not use log4j, you can change log.info () to system. Out. println () to get the same result.
If you want to configure log4j, click log usage

If you have any questions, please contact me: webmaster@bcxy.com

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.