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 How can I redirect from sessionDestroyed() of Session Listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can I redirect from sessionDestroyed() of Session Listener" Watch "How can I redirect from sessionDestroyed() of Session Listener" New topic
Author

How can I redirect from sessionDestroyed() of Session Listener

Anurag Pathak
Greenhorn

Joined: Jun 08, 2009
Posts: 22
Hi,

I have written Following ClassIts working fine and I am getting sessioncreated() and session destroyed() events as session is created and destroyed.

//Below is the Code//
package org.genx.sessionlogout;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;

public class SessionCounter extends HttpServlet implements HttpSessionListener {
private static int activeSessions = 0;

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("Inside doGet TTTTTTTTTTTTT");
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

System.out.println("Inside doPost TTTTTTTTTTTTT");
}

public void sessionCreated(HttpSessionEvent se) {

System.out.println("Session Created Anurag");

activeSessions++;
}

public void sessionDestroyed(HttpSessionEvent se) {

try{

if(activeSessions > 0){
activeSessions--;
}
HttpServletResponse res= null ;
String contextPathName=null;
ResourceBundle contextPath = ResourceBundle.getBundle("contextPath");
contextPathName = contextPath.getString("contextName");

res.sendRedirect(contextPathName+"/jsp/home/InvalidLogin.jsp");



}
catch(Exception e){
e.printStackTrace();
}

}



public static int getActiveSessions() {

return activeSessions;
}

}


As soon as i try to redirect it to another page it gives null pointer Exception actually I have to redirect it to another jsp page as sson as sessionDestroyed() is called.

How is it possible.

All ideas are welcome.

Looking forward for reply.

Regards

Anurag Pathak

pathak_arg@yahoo.com
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 288

Anurag

Session destroy event will happen when client(Browser) was closed.

But how can you redirect to other JSP page?


-Chiru
dhaval yoganandi
Ranch Hand

Joined: Jun 06, 2009
Posts: 35

Its giving you null pointer because you've created a request object with null reference. And you're using it to redirect to a page. That's why it is giving you NPE(Null Pointer Exception). Your request req object must be initialize first before using it.

You cannot do some thing like..



It should be


a.k.a stalker.
Anurag Pathak
Greenhorn

Joined: Jun 08, 2009
Posts: 22
Yes Sir,

But how is it possible please give me a small example
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you don't have a request from a client (and you won't when the session is destroyed) why redirect anywhere? Think about it: what are you redirecting? And for whom?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
dhaval yoganandi
Ranch Hand

Joined: Jun 06, 2009
Posts: 35

Actually sessionDestroyed() method don't have HttpRequest as an argument so you won't get HttpRequest object in that method.
Anurag Pathak
Greenhorn

Joined: Jun 08, 2009
Posts: 22
Hi,

Its actually the client requirement as session is expired redirect it to another page.

which can only be triggered throgh sessionDestroyed().

is there any other way round
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

Sorry, just saying that it's a requirement doesn't mean that it makes any sense at all. And in this case it doesn't.

When the client's session expires, the client hasn't connected to your site for the last 30 minutes (or whatever your timeout period is). So there isn't any request to be redirected, as has already been said at least twice.

And even if the client's browser is still running (which it might not be), the client isn't going to be happy if your application popped up over their Youtube viewing to inform them of the irrelevant fact that their session had expired. It wouldn't be a good idea to tell your clients that this is a stupid requirement (although it is) but you do have to explain to them that it can't be implemented and that it shouldn't be implemented.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

hey there, you should probably revisit the design, you should not redirect to any page after the session has been destroyed. You could probably check in any action class if the request contains the credentials or your session object still has it and only then you redirect it to the proper page and not the other way.

However you can use the jsp <whatever you use>. Request can never be "null" - even if it is expired... but once again.. its time to revisit the design.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I redirect from sessionDestroyed() of Session Listener
 
Similar Threads
redirect implementation with URL class
HttpSessionListener
How to test HttpSessionListener
Session counting problem
Servlets example