3

How to alert user before session expires in Java?

In StackOverflow this question is discussed so many times. But my question is slightly different. I have a requirement to modify the existing application. My application is very big. Session will be expired after 30 mins if there is no response from the user. I want to display the alert before session expires, say at the 25th min I would like to display a prompt to user about session expires

In my application, more than 2000 jsp's are used and no common js is used. So I cannot change all the jsp file to include the javascript code for checking the session timeout.

How to handle this?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Elayaa
  • 31
  • 1
  • 7
  • 1
    It would probably be best to do this on the client-side in Javascript not Java. – Stephen C Sep 02 '13 at 14:36
  • @Stephen C: Thanks for your response. My application having more than 2000 files. I cannot create a javascript code in all the 2000 pages. – Elayaa Sep 02 '13 at 14:43
  • 1
    @Elayaa : you can keep one javascript file in the server and call it in every page – Hussain Akhtar Wahid 'Ghouri' Sep 02 '13 at 14:49
  • Or you can "include" one JSP fragment into all JSPs. (I assume you have attempted some modularity in your JSP design; e.g. common include files to provide common headers, etcetera.) But either way, this does need to be done client-side. If that means tweaking 2000 JSPs, so be it. (Seriously ... 2000? What is this monster you have created?) – Stephen C Sep 02 '13 at 14:52
  • @HussainAkhtarWahid: Sorry I am very new to this. Is there any sample codes available? Send me the URL. Also If i keep the JS file in server side? How can I prompt the warning message in client side? – Elayaa Sep 02 '13 at 15:05
  • @StephenC: I didnt create a monster stef :-) Its a enhancement to the already existing application. Modifing 2000 files is very complex. Is there any other way to do without modifing the existing 2000 files? – Elayaa Sep 02 '13 at 15:09
  • @StephenC: The jsp's header and all coming from Interwoven(content Management system). No common files are used – Elayaa Sep 02 '13 at 15:11
  • @Elayaa - Put the common stuff into the Interwoven headers then. Surely it allows you to do that! – Stephen C Sep 02 '13 at 16:15
  • @StephenC: Thanks. I have modified the common jsp in interwoven and it is really working:-). Now I have another question, suppose if the session expire time is 30 mins, i have modified the code to display the alert at 25th min. Alert will have two buttons "Ok" to continue wit the session and "Cancel" to logout. If suppose the user noticed the alert after 30 mins, even if they click on ok it should not allow them. It should display "your session expired" alert. How to handle this? Also If the user noticed the alert before session expires, need the user to continue wit same session. How to do? – Elayaa Sep 05 '13 at 02:09
  • @Elayaa - Ask a new Question. – Stephen C Sep 05 '13 at 08:12
  • Thanks all. I have completed this task. Modified the common file in interwoven to alert user at 25th min if user is idle. Then a ajax request sent to server if user clicks ok. If the session is valid, we will get response from ajax.So the session is renewed. If there is no response means session already expired. So logged out the user. – Elayaa Oct 19 '13 at 05:46

2 Answers2

3

Create an iframe, and put the jsp pages inside there.

In the top page, that the iframe is within, then have javascript monitor for the jsp page changing (https://stackoverflow.com/a/1930942/67566).

If the jsp page hasn't changed in 25 mins you can warn the user.

If you are using ajax calls within your jsp pages then this will be more problematic, but the fact that you have so many jsp pages leads me to assume you aren't.

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166
-1

Use SessionListeners.

By implementing HttpSessionListener you can alert the user before the session is going to be expired.

Have a look at this sample example

EDIT:

The following are two methods you can override, sessionCreated(HttpSessionEvent se) and sessionDestroyed(HttpSessionEvent se) They are called when the session is created and when the session is destroyed respectively. Here is a sample snippet.

      public void sessionCreated(HttpSessionEvent se)
{
    HttpSession session = se.getSession();
    System.out.print(getTime() + " (session) Created:");
    System.out.println("ID=" + session.getId() + " MaxInactiveInterval="+  
   session.getMaxInactiveInterval());
  }
public void sessionDestroyed(HttpSessionEvent se)
{
    HttpSession session = se.getSession();
    // session has been invalidated and all session data 
    //(except Id)is no longer available

    System.out.println(getTime() + " (session) Destroyed:ID=" 
   + session.getId());
}
private String getTime()
{
    return new Date(System.currentTimeMillis()).toString();
}

You can write your logic in those methods.

MaheshVarma
  • 2,081
  • 7
  • 35
  • 58
  • HttpSessionListener will call once the session is created or destroyed. ButI want to display the message before the session expires. How to do this? – Elayaa Sep 02 '13 at 14:51
  • @MaheshVarma your answer is not correct. he wants to show some popup on the client side Before the sessio is expired!.. – john Smith Jan 07 '14 at 14:42
  • It is incorrect answer to OP. but it may be useful for someone who is looking for `SessionListeners`. @johnSmith – MaheshVarma Jan 08 '14 at 03:07
  • I think we can implement by using session listeners and `Timer` class in java. – MaheshVarma Jan 08 '14 at 03:08