Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Probelem when Forwarding when session expires

 
Naidu Jitta
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Praful's code in a Filter.
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic