A tip to determine if the session is out of date

Source: Internet
Author: User

There were several questions earlier about how to determine if the session was out of date.
Contact Jspo has been a long time, but really did not think about this problem, then did not find a satisfactory answer to others.
At that time, I mentioned a way to implement the Httpsessionlistener interface, and then perform some operations in the sessiondestroyed () method, indicating that the session is going to expire because this method is Session.invalidate () The method that the listener called before. The method of judging is obviously not satisfactory.
I asked some people, but I didn't get an accurate answer.
Just by chance to see, Request.getseesion (Boolean) method, suddenly let me suddenly dawned. This method has a Boolean value that, if true, creates a new session if the current request session is unavailable, and returns the current session if it exists. If the argument is false, then NULL is returned when the current session of the request does not exist.
So we can easily associate with this so-called request that the current session exists and the session expires, so we can "approximate" that the session does not exist or the session expires, Then we can easily determine if the session is out of date. Here's how:
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 special case is the first time the request has not yet created a session, then the return of this method is still null, the reason I think everyone should be obvious.

Above is a personal humble opinion, I hope you correct me.

Note:

Author Jfresh_man (the answer is ...) (God said to me: programming to all-round development,)


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.