1

If the browser cache is cleared , the current session will be unused and new session will be created.but according to my situation when session is unused i want to call java method .

My aims is once the browser cache is cleared, the old session remains unused and new session created before that i want call a method.

Is there any way to do so or my way of thinking is wrong please correct me.

Its not same as How to call a method before the session object is destroyed? this question , because there after timeout that session is getting destroyed but to my situation the session is not destroyed by any app server .The session remain unused due to browser clear cache .

goku
  • 223
  • 2
  • 12
  • 2
    clearing the browser cache does not imply that the server session is destroyed immediately – wero Feb 23 '16 at 14:26

1 Answers1

0

In Java, we usually do this using a Listener, in this case an javax.servlet.http.HttpSessionListener to check the event of sessionDestroyed and sessionCreated:

package com.mkyong;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionCounterListener implements HttpSessionListener {

    private static int totalActiveSessions;

    public static int getTotalActiveSession(){
        return totalActiveSessions;
    }

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        totalActiveSessions++;
        System.out.println("sessionCreated - add one session into counter");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        totalActiveSessions--;
        System.out.println("sessionDestroyed - deduct one session from counter");
    }   
}

After create the class, you need to add your listener class in the web.xml:

<web-app ...>
    <listener>
        <listener-class>com.mkyong.SessionCounterListener</listener-class>
    </listener>
</web-app>

All this references an explanations I got from here here: Mkyong - A simple HttpSessionListener example

Brother
  • 2,150
  • 1
  • 20
  • 24