Whether the user is online (single-point login)

Source: Internet
Author: User
1. How to determine whether the session has expired.
I have been in contact with JSP for a long time, but I have never thought about this question. At that time, I didn't find a satisfactory answer.
At that time, I mentioned a method to implement the httpsessionlistener interface, and then execute some operations in the sessiondestroyed () method, which indicates that the session will expire because the method is session. method called by the listener before invalidate. This judgment method is obviously not satisfactory.
I asked some people, but I didn't get an accurate answer.
I suddenly realized that the request. getseesion (Boolean) method. In this method, a Boolean value is passed. If this value is true, a new session is created if the current request session is unavailable. If yes, the current session is returned. If the parameter is false, null is returned if the current session of the request does not exist.
In this way, we can easily think of whether the current session of this so-called request has a connection with the session expiration, so we can "approximately" think that the session does not exist because the session has expired, then we can easily determine whether the session has expired. The method is as follows:
If (request. getsession (false) = NULL)
System. Out. println ("session has been invalidated! ");
Else
System. Out. println ("session is active! ");

You may have noticed that there is an "Approximate" word above, that is, there are special situations.
In this special case, when the first request has not yet created a session, the returned value is still null in this method. I think it should be clear to everyone.
Httpservletrequest request = (httpservletrequest) executions. getcurrent (). getnativerequest ();
Httpsession SSN = request. getsession (false );
SSN. setattribute ("name", value );

2. Session destruction
1) Ajax
<Body onUnload = "method ()"> This method cannot be used to delete a session.
2) The Single-point login on the Web end is mainly implemented by recording JSESSIONID to manage sessions, but there are still some problems that cannot be accurately implemented to delete sessions when users exit.

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.