• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Can I access session object even session is time out?

 
stshiva raaj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks, Can I access the session object even session has been expired? I need to check whether session is expired or not for each request. How to check?
The session invalidation is set null the session object. What I concluded, session time out I can access session object but session invalidation I can not access session object. Am I correct? Then How can I find the
session time out by using session object?
 
Daniel Val
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stshiva raaj wrote:Hi folks, Can I access the session object even session has been expired? I need to check whether session is expired or not for each request. How to check?
The session invalidation is set null the session object. What I concluded, session time out I can access session object but session invalidation I can not access session object. Am I correct? Then How can I find the
session time out by using session object?


I know what you mean - you login, store something in the session and then you want to look it up and see if it is there. If not, that means the session expired.


Yes, you can do that: just access the object. session.getAttribute("name") etc, and you get null if the object is not there.


I know your concern - other languages / techlologies you need first to check whether the object is there and then retrieve it or else you get a warning. Not here. Just don't try to work with the object if it is null.


D
 
stshiva raaj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Deniel. I understand but my question is going to be straight forward. How Can I find whether session is time out or invalidate?

If session is null - session is invalidated
if(session ==null)
logger.info("Session is destroyed");

If session is not null but how to check time out or not
if(session !=null && ??)//??-how to check session is time out or
logger.info("Session has been expired");

FYI : Even session has been time out,the session object is not null. Then how I find the session is expired or not?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to check the session timeout status at each request?
If you want to do something when the session is destroyed then implement http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionListener.html
 
stshiva raaj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Armitage, Actually my question is, even session time out happen why session instance is not null. Using session instance i can access some session properties. Is there any API to find the session time out without help of HttpSessionListener
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSession.html#getMaxInactiveInterval%28%29 which will tell you how long the server will keep this session opened.

You are very likely trying to do something that you shouldn't be doing though. What is your exact requirement? Why is implementing the session listener interface not adequate?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stshiva raaj wrote: Is there any API to find the session time out without help of HttpSessionListener


Why are you avoiding an API which is meant for cases like what you seem to be after?
 
stshiva raaj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Are you sure after session has been expired sessionDestroyed()will call right?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stshiva raaj wrote:Thanks. Are you sure after session has been expired sessionDestroyed()will call right?


The servlet spec mandates that the session listener be called on a session timeout.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based upon what's been said here, I agree with E. I think you are focused too much on the session specifics. What are you actually trying to accomplish?
 
stshiva raaj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.I am going to ask final question.

If session.invalidate() -- session is null
and
If session has been expired -- session instance is not null right?

If not null means, How can I check the session has been expired or not?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are just repeating the same question without even answering the questions that have been asked to you. Like:


What are you actually trying to accomplish?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!