In the jsp project, the browser is closed, and the browser remains online as long as the browser is not closed.

Source: Internet
Author: User

In a recent project, jsp (the preferred choice for SUN Enterprise Applications) is used to achieve user logon, and session persistence is of course used. This is implemented using the implements HttpSessionBindingListener interface. This is the brief code I wrote.
Public class OnLineUser implements HttpSessionBindingListener {
Private NewUser newUser = null;
Public OnLineUser (){
NewUser = new NewUser ();
}


Public boolean existUser (String str)
{
Return false;
}

Public void valueBound (HttpSessionBindingEvent e ){
String tempName = e. getName ();
// Integer m = new Integer (tempName );
NewUser. setUserName (tempName );
Try {
NewUser. updateOnline ();
}
Catch (Exception ex ){
}
System. out. println ("user" + tempName + "upper limit ");
}

Public void valueUnbound (HttpSessionBindingEvent e ){
String tempName = e. getName ();
// Integer m = new Integer (tempName );
NewUser. setUserName (tempName );
Try {
NewUser. updateUnline ();
}
Catch (Exception ex ){
}
System. out. println ("user" + tempName + "lower limit ");
}
}

Call
OnLineUser onLineUser = new OnLineUser ();
Request. getSession (false). setAttribute (login_no, onLineUser );
Login_no indicates that the variable is the logon name. At the same time, set request. getSession (false) to false, which has two values: true and false. true is created if there is no same session. If yes, It is overwritten. If no, it is created. If yes, It is not created. I use this method to implement Single-point logon. Therefore, false is used. In this way, you can listen to the session you created. If the session times out, the valueUnbound method is executed. Create a session and run the valuebound method. Here, I want to change a person's status field. In this way, when this user is online and another user is online on another machine, I can make a judgment on the login page. There are many materials on the Internet, and the problem is that when the session timeout time is set to 1 minute. If the user does not exit normally, the user can change the offline status after one minute. However, if the user does not perform any operations on the computer within one minute, the user will also be suspended. I have been searching online for a long time, no such information is available. Later, I used my framework (my program uses the framework, and I use the bottom framework)
Javax. servlet. http. HttpSession session2 = request. getSession (false );
Com. chinantn. oa. util. OASession oasession =
(Com. chinantn. oa. util. OASession) session2.getAttribute ("oasession ");
This is a session processing class used on my login page. Then add
<Meta http-equiv = "refresh" content = "30; URL = foot. jsp (preferred for SUN Enterprise Applications)">. auto refresh.
The interval is less than the session Timeout time. The main purpose is to solve the problem by automatically refreshing the Access session if the page is not accessed within the timeout period.

Because I have not found any article on the Internet to solve this problem for a long time, I want to write out the solution I want to solve it later. I hope it will help those who have encountered this kind of problem. If you have any questions, contact me, ljunjie@gmail.com.

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.