File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Probelem when Forwarding when session expires Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Probelem when Forwarding when session expires" Watch "Probelem when Forwarding when session expires" New topic
Author

Probelem when Forwarding when session expires

Naidu Jitta
Greenhorn

Joined: May 12, 2003
Posts: 14
I am using HTTPSessionListner. So when where session expires I need to redirect the page to the home page. Here is my code:
public final class SessionWatcher implements HttpSessionListener {
RequestDispatcher req;
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
String sid = session.getId();
long time = session.getCreationTime();
System.out.println("************************New sessionID: " + sid+"************************");
System.out.println("************************Session created @ " + time+"************************");
req = event.getSession().getServletContext().getRequestDispatcher("/login.jsp/?TS3Constants.MESSAGE=TS3Constants.SESSION_EXPIRED");
System.out.println("************************RequestDispatcher req ==>" + req.toString()+"************************");
}
public void sessionDestroyed(HttpSessionEvent event) {
System.out.println("************************Session " + event.getSession().getId() + " destroyed************************");
try{

System.out.println("************************RequestDispatcher req ==>" + req+"************************");
req.forward(null, null);
}
catch(Exception e){
System.out.println("************************Session " + event.getSession().getId() + " destroyed************************");
e.printStackTrace();
}

}
}
This is giveing the Null pointer exception. I am able to to print the request dispatcher. even though I am getting Null Pointer when I am forwarding.
Help me in this regard...
Is there any other way to proceed with the Listner itself???

Thanks in Advance...
J. P. Naidu
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12836
    
    5
I don't think that trying to keep an instance of RequestDispatcher hanging around until the session expires is a good idea - it may even be impossible. You might be able to keep a reference to the ServletContext, since it is not going to be changing.
Bill
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
I know this is a old topic. Can anybody tell me how to achieve this?
I mena, I want to redirect the browser to login page when the session times out. How can i do that?
Using sessionListener is no use as i dont get the request or responce object there. So cannot use request dispatcher.
Please help ASAP.
thanks in advance.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
Hi Rashmi,
I know this is very simple way of doing it & there may be some other better solution,still chek out if this helps you.
In all your jsp or servlets put following line at starting.
if(session is invalidated....or lost)
{
redirect to login page
return;
}

cheers
Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Hi praful,
The problem is there are 100 odd jsp pages in the application and we want to minimize the impact of this change. so this won't be a feasible solution. Thats why , i thought, using sessionListener would be a good option.
Anyways, thanks for helping.
Can anybody suggest me more?
rick zorich
Greenhorn

Joined: Aug 31, 2003
Posts: 16
You could use Praful's code in a Filter.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
Hi Rashmi,
Well I wont be able to put this thing very clear...just trying to give you some idea you will have to do some RND....here I go....
You can write one servlet which will be called for every request in your application ,it will checks for session validity,if session is no valid it will redirect to login page else to jsp page which was supposed to be called.
Here you just need to change your web.xml in your weapps folder as follows....
Add following tags

Hope this helps
Cheers
Praful
[ February 17, 2004: Message edited by: Praful Thakare ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Probelem when Forwarding when session expires