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,) |