Results 1 to 7 of 7
  1. #1
    sakshi.13agg is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default HttpSessionListener and Session Invalidate

    I want to override method sessionDestroyed of interface HttpSessionListener .
    And my purpose f doing so is that whenever I call session.invalidate() from my server code then a condition should be checked and if that condition is false then session should not get invalidate.It should persist on web server.

    Can anyone please let me know if this can be done??

  2. #2
    farrukh is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: HttpSessionListener and Session Invalidate

    First of all, You will be implementing not overriding the method sessionDestroyed. I know these implemented methods are sometimes annotated as @Override but to me that is simply wrong.

    Coming to your actual question, The sessionDestroyed method is basically just a way to notify the destruction of session object and for the clean up purpose. Think of it in terms of the code that calls the sessionDestroyed method. It will be something like this

    if (invalidate == true ) {
    listener.sessionDestroyed(event);
    }
    session=null;

    The only way to stop this code from executing further is by interrupting or stopping the current thread from within sessionDestroyed method. It is not ideal but should work.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,862
    Rep Power
    19

    Default Re: HttpSessionListener and Session Invalidate

    How about not invalidating the session in the first place?

    This smacks of a problem in your design/architecture rather than something you should be trying to shoehorn a hack into.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    sabarii is offline Member
    Join Date
    Sep 2011
    Location
    Pondicherry
    Posts
    7
    Rep Power
    0

    Default Re: HttpSessionListener and Session Invalidate

    you cant override session invalidate method. HttpSessionListener is called only when creation and destruction of session. Do one thing, write seperate method for invalidate session.

  5. #5
    sabarii is offline Member
    Join Date
    Sep 2011
    Location
    Pondicherry
    Posts
    7
    Rep Power
    0

    Default Re: HttpSessionListener and Session Invalidate

    you cant override session invalidate method. HttpSessionListener is called only when creation and destruction of session. Do one thing, write separate method for invalidate session.

  6. #6
    farrukh is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: HttpSessionListener and Session Invalidate

    Here is a listener code i wrote to test my theory. It will stop the session from destroying upon invalidation or session expiration. However, As Toll mentioned above, it is just a hack and not the standard way to approach this problem. You should consider modifying your application design. If you want better control over your session expiration then don't let the sessionManager expire it but invalidate it from within your application.

    public class TestListener implements HttpSessionListener {

    public TestListener() {
    }

    public void sessionCreated(HttpSessionEvent arg0) {
    System.out.println("session created with session id "+arg0.getSession().getId());
    arg0.getSession().setMaxInactiveInterval(5);
    }

    public void sessionDestroyed(HttpSessionEvent arg0) {
    System.out.println("sessionDestroy called for session id"+arg0.getSession().getId());
    Thread.currentThread().stop();
    System.out.println("You should not see this statement");
    }

    }

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,862
    Rep Power
    19

    Default Re: HttpSessionListener and Session Invalidate

    There's a reason stop is deprecated.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to Invalidate back navigation in browser after session invalidation
    By krishnareddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-31-2012, 08:17 AM
  2. Replies: 13
    Last Post: 07-19-2010, 11:04 AM
  3. How to detect browser closing to invalidate a jsp session
    By kumar_ps78 in forum JavaServer Pages (JSP) and JSTL
    Replies: 13
    Last Post: 08-13-2009, 03:56 PM
  4. Using HttpSessionListener
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 03:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •