5

I have a dynamic web project in Java using Hibernate, servlets and JSP. I want to get the remaining session time for a client so that I can warn them of the upcoming session expiration.

For example, the user would login to the application with one minute of session time. When the remaining session time is ten seconds, I want to warn the user using AJAX and JQuery. How would I do this?

Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
sopehl
  • 1,067
  • 1
  • 13
  • 18
  • I expect you would need to just run a timer on the webpage. It's not like you can query the server because I believe that would reset the session timer anyway (maybe, I am not 100% sure what exact actions reset the session timer)... [maybe this is worth reading](http://stackoverflow.com/questions/10370110/does-session-timeout-reset-on-every-request) – musefan Aug 07 '14 at 12:06
  • 4
    Stack overflow isn't here to write code for you. Please attempt to solve this problem yourself, write some code, and then if you get stuck post the code you have and a **specific** issue you have with that code and people will help you with it. – Styphon Aug 07 '14 at 12:38
  • I had actually asked [a similar question](http://stackoverflow.com/questions/15147118/displaying-timeout-warning-within-jquery) that would certainly apply in your situation (since the main timeout logic is done in JQuery) – Chris Forrence Aug 07 '14 at 15:06

2 Answers2

8

You can make use of HTTPSession.

  • getLastAccessedTime() : returns the time session was last accessed
  • getMaxInactiveInterval() : returns the timeout period.

Make use of current time and apply logic.

Community
  • 1
  • 1
Jayarajan
  • 146
  • 6
2

Well you cannot actually get session time with an AJAX request as the request it self will also reset the session timeout time. What you can do is get it when you are rendering the page in a js function and start a counter with setTimeout when and show your message pop when less then 30 seconds left.

Onur Topal
  • 3,042
  • 1
  • 24
  • 41