aspose file tools*
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
Author

forward on session timout

A Saari
Greenhorn

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

javax.servlet.http.HttpSessionBindingListener

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 ...
Amy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

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] [Bear's FrontMan] [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
Greenhorn

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

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

chain.doFilter (request, response);

...

Thanks for any help.

Amy
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 ]

Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
A Saari
Greenhorn

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 ...

amy
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
Sheriff

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 http://simple.souther.us/not-so-simple.html has code that does this.


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

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

Perfect. Thank you so much!

Amy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: forward on session timout