File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Session Timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Timeout" Watch "Session Timeout" New topic

Session Timeout

Sri Ram

Joined: Feb 13, 2006
Posts: 20
I have a session time out configured in the web.xml.In my filter I use request.getSession(false).Then I check the value in my session object.But even after the session has expired my session object does not get a null value.

Can you tell me how to check if my session has expired?

Sriram<br />SCJP<br />SCWCD
Hendy Setyo Mulyo
Ranch Hand

Joined: Dec 01, 2004
Posts: 219

How did you set the session timeout in web.xml? Can give the snippet?

Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Jetendra Ivaturi
Ranch Hand

Joined: Feb 08, 2007
Posts: 159
session.getSession(false) will return pre-existing session. It returns null only when there is no session associated with.

we have some methods like


by using getLastAccessedTime() we get some time in milliseconds

and if it exceeds the time you have mentioned(<session-timeout> in web.xml(that represents hours) call invalidate() so that the session will be destroyed.

hope this works.

SCJP 1.4 & 1.5, SCWCD 1.5. Learn and Let Learn.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I am sure you session is not expired.You can try setting a session listner for tracking whether session has really expired or not.

Rahul Bhattacharjee
LinkedIn - Blog
Ryan Day
Ranch Hand

Joined: Apr 03, 2006
Posts: 87
Originally posted by Jetendra Ivaturi:
(<session-timeout> in web.xml(that represents hours)


Is actually minutes.

Soma Kora

Joined: Apr 10, 2007
Posts: 6

Here I am facing one problem. I am checking session validity

1: if (! request.isRequestedSessionIdValid())
return mapping.findForward("invalidSession");

2: user = (User)request.getSession().getAttribute("user");
if (user == null){
// throw exception if it is null;
throw new ApplicationException("User session unavailable");

Problem: In line i get true for request.isRequestedSessionIdValid() but when i am trying to get value from session(ie line 2) it is giving "User session unavailable")?

Can you pls suggest where went wrong?

Jilani Jidni

Joined: Feb 06, 2007
Posts: 8
Hello kora,

You are checked for session availability and session is available. But you try to get session with attribute "user". Please check session with user attribute are available in your session. otherwise check whether you set your session.

With Regards<br /> Jilani Jidni<br /> Bangladesh
I agree. Here's the link:
subject: Session Timeout
jQuery in Action, 3rd edition