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

forward on session timout

A Saari

Joined: Mar 31, 2006
Posts: 26
Hi All

Can anyone tell me how to forward to an error page when the session has timed out?

I implemented


public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent event) {
// what code can I put in here to forward to
// my error page sessiontimeoutError.jsp ???

Thanks for any thougths ...
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

You can't since at the time that the listener is triggered you have no idea what's displayed in the user's browser or if they're even still looking at your site. Chances are they're long gone and browsing ebay or something...

The purpose of the listener is so that you can clean up after yourself if need be when the session times out.

If you want to detect that a session has timed out when a user hits one of your pages or controllers, that's a good job for a servlet filter.

[Asking smart questions] [About Bear] [Books by Bear]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I use a site that has javascript on every page that pops a dialog box in the browser after n minutes. If your user is doing absolutely nothing but letting one of your pages sit there, that could warn them they are about to time out. If they wander off to Google or some other site, that script will be gone of course.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
A Saari

Joined: Mar 31, 2006
Posts: 26
Bear - could you please help me just a little bit more?

I've created the filter class you suggested and added it to web.xml - it gets called just fine, but I don't understand how to figure out if the session has timed out and then redirect (can I use RequestDispatcher?) to the sessionTimedOut.html page I want to display.

public void doFilter (ServletRequest request,
ServletResponse response,
FilterChain chain)

// what code goes here ???
ServletContext sc = filterConfig.getServletContext();

chain.doFilter (request, response);


Thanks for any help.

Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Saari, the filter has access to the request object. can't you just check there.
[ April 26, 2006: Message edited by: Bosun Bello ]

So much trouble in the world -- Bob Marley
A Saari

Joined: Mar 31, 2006
Posts: 26
well, yeah, i would have thought so but there's no getSession() method on the request object passed in - so i don't know how to gain access to the session object via the request object ...

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

but there's no getSession() method on the request object passed in -

You can always down cast the interface type

ServletRequest req;
HttpServletRequest request = (HttpServletRequest) req;

HttpSession session = request.getSession();

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

I do this by adding an object (userBean) to the session upon a sucessfull login. My filter checks for the existence of this object with each hit to the site. If the object doesn't exist in session (session.getAttribute("userBean" ) returns null) then I redirect the user back to the login screen.

The SessionMonitor demo app on has code that does this.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
A Saari

Joined: Mar 31, 2006
Posts: 26
Vishnu & Ben

Perfect. Thank you so much!

I agree. Here's the link:
subject: forward on session timout
It's not a secret anymore!