A trick to determine if a session is out of date

Source: Internet
Author: User
session| Tips

There were several questions about how to determine whether the session expired.
Contact with JSPO has been a long time, but really did not think about this problem, then did not find a satisfactory answer to the others.
At the time, I mentioned a way to implement the Httpsessionlistener interface and then do some action in the sessiondestroyed () method to indicate that the session is going to expire because this method is Session.invalidate () The method that the listener called before. This method of judgment is clearly not satisfactory.
I asked some people at the time, but I didn't get an accurate answer.
Just by chance, the Request.getseesion (Boolean) method suddenly dawned on me. This method has a Boolean value that, if true, creates a new session if the current session of the request is not available, and returns the current one if it exists. If the argument is false, NULL is returned when the current session of the request does not exist.
In this way we can easily associate this so-called request with the current session and the expiration of the link, so we can "approximately" think that the sessions do not exist is to expire, Then we can easily determine if the session is out of date. The method is as follows:
if (Request.getsession (false) ==null)
SYSTEM.OUT.PRINTLN ("Session has been invalidated!");
Else
System.out.println ("Session is active!");

It may be noted that I have an "approximate" word above, which means there is a special case.
This particular case is when the first request has not yet been created, and the return of this method is still null, and I think we should all be obvious.

The above is a personal humble opinion, I hope you will correct me.



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.