Link to home
Start Free TrialLog in
Avatar of spiderman19
spiderman19

asked on

HttpSession related

hi all,

i have this in my jsp page and within my servlet.

userObject userSession = (userObject)session.getAttribute("userSession");

in my tomcat, i have set in web.xml to this

<session-config>
      <session-timeout>50</session-timeout>
</session-config>

Now i wish to display some sort like indicator to the user when the session is ABOUT to end and the system will redirect to logut.jsp screen.

Any ideas how to perform this?

I have read the docs, i can only see this is useful, yet it's bring no meaning to me if i do this

<%= session.getMaxInactiveInterval() %>

Avatar of boonleng
boonleng
Flag of Malaysia image

Session timeout means that there's not interaction between the user and the server.
So if you want to provide some alert to the user when session is about to timeout, you would need to do this using javascript setTimeout().
Example:

<script type="text/javascript">
    setTimeout("alert('Session about to timeout.')", <%= (session.getMaxInactiveInterval() * 1000) - 10000 %>);
</script>

session.getMaxInactiveInterval() return in seconds, whereas setTimeout() using miliseconds, so need to multiply by 1000.
then minus the time (e.g 10000 miliseconds) before session timeout for the alert to be prompted.

Hope this can help.

Regards,
Boon Leng
Avatar of spiderman19
spiderman19

ASKER

Boon Leng, thanks for the comments. Wondering how can i call this setTimeoout? if put within the <body> tag or with onClick version?

But to make it sound more sense, i think should keep it display somewhere in the page.. to constantly showing the user, how many times left if he didnt give a respond back.

But what do you think?
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry the function calculateTime should look like

function calculateTime()
{
   if (remainingTime > 0)
   {
         window.setTimeout("calculateTime", 1000);
         displayTime();
         remainingTime = remainingTime - 1;
   }
}

arunrs
I still beat you to it ;)
Small typo error. It should be

window.setTimeout("calculateTime()", 1000);
instead of
window.setTimeout("calculateTime", 1000);

arunrs
You sure did Michel.:)

Also it is sensible to display the remaining time in window rather than in status bar. :)
ok guys..

let me try each of your ideas and got any problems will come to you again
hi, this is the code i have in my page:

<script type="text/javascript">
timeToLeave = <%= (session.getMaxInactiveInterval() * 1000) - 170000 %>;
secs = parseInt(timeToLeave/1000);
function showTimer() {
  var show=document.getElementById('show');
  secs--
  var plur=(secs==1)?'':'s';
  if (timeToLeave<0) secs*=-1;
  if (show) show.innerHTML='Your session '+(timeToLeave >0)+'will timeout in '+secs+' second'+plur+'timedout '+secs+' second'+plur+' ago';
}
setInterval('showTimer()', 1000);
</script>

with some amendment that i have add in to sovle some typo errors.

But i wish to change to this, * to redirect the entire page to "login.jsp". Is that suppose to code like that? bt it's not working.


  if (show && timeToLeave>0 )  {
  show.innerHTML='Your session '+(timeToLeave >0)+'will timeout in '+secs+' second'+plur+'timedout '+secs+' second'+plur+' ago';
if (timeToLeave=0) {
        window.location="/login.jsp";
  }
}

but it's not working on the second part...

i means the window redirecting doesnt work
syntax.

if (timeToLeave==0)

Please look here:
http://www.w3schools.com/js/default.asp
Anyway I meant


  if (show) show.innerHTML='Your session '+(timeToLeave >0)?'will timeout in '+secs+' second'+plur:'timedout '+secs+' second'+plur+' ago';

sorry.

So the script is



<script type="text/javascript">
timeToLeave = <%= (session.getMaxInactiveInterval() * 1000) - 170000 %>;
secs = parseInt(timeToLeave/1000);
tId="";
function showTimer() {
  if (timeToLeave==0) {
    clearInterval(tId);
   window.location.replace("/login.jsp");
  }
  var show=document.getElementById('show');
  secs--
  var plur=(secs==1)?'':'s';
  if (show) show.innerHTML='Your session will timeout in '+secs+' second'+plur

}
tId= setInterval('showTimer()', 1000);
</script>
mplugin, this is my final code: Everything work fine excecpt the redirecting part..

<script type="text/javascript">
alert( <%= (session.getMaxInactiveInterval() * 1000) - 115000 %> );
timeToLeave = <%= (session.getMaxInactiveInterval() * 1000) - 115000 %>;
secs = parseInt(timeToLeave/1000);
tId="";
function showTimer() {
  if (timeToLeave=0) {
    clearInterval(tId);
   window.location.replace("/login.jsp");
 }
  var show=document.getElementById('show');
  secs--;
  var plur=(secs==1)?'':'s';
  if (show) show.innerHTML='Your session will timeout in '+secs+' second'+plur;

}
tId= setInterval('showTimer()', 1000);
</script>

i have try to change couple of things.. but then still not able to see the page (login.jsp) come out. nothing at all.
TWO equal signs - why do you insist on removing one of them?

if (timeToLeave==0) {

a=0; // setting a to 0

if (a==0) // asking if a is equal to 0


Michel
ok.. my mistake.

but then even i add in back the "==" sign, its still didnt redirect to the login.jsp
Hi,

I have a doubt...
Is it

if (timeToLeave==0) or is it

if (secs==0)  ??

arunrs
And I am sorry I did not pay attention:

  if (secs<=0) {

not

if (timeToLeave==0) {
Better:

  if (secs<=0) {
    clearInterval(tId);
    window.location.replace("http://www.google.com");
    return;
 }
BINGOOOOOOOOOOOO

but guys...

my page is having frame....

means, it's something like this...

-----------------------------------------
        top.jsp
----------------------------------------
              |
              |
 left.jsp   |    main.jsp
              |
              |
              |


the www.google.com only affect the main.jsp as my main.jsp is there..... how to reflect the entire page when secs ==0?
Hi

If that is the case..
then use

parent.location.replace("www.google.com");

arunrs
that would be

top.location.replace("http://www.google.com")
unless you only have one level of frames.

But what do you mean by

the www.google.com only affect ....
this is my final code. Both. parent.location or top.location is work either way.


<script type="text/javascript">
alert( <%= (session.getMaxInactiveInterval() * 1000) - 1195000 %> );
timeToLeave = <%= (session.getMaxInactiveInterval() * 1000) - 1195000 %>;
secs = parseInt(timeToLeave/1000);
tId="";
function showTimer() {



  if (secs==0) {
  clearInterval(tId);
    top.location.replace("login.jsp");
    return;
 }
  var show=document.getElementById('show');
  secs--;
  var plur=(secs==1)?'':'s';
  if (show) show.innerHTML='Your session will timeout in '+secs+' second'+plur;

}
tId= setInterval('showTimer()', 1000);
</script>

Anyway, thanks for your both help.
Hi,

I have one more doubt...

It is mentioned that "<%= (session.getMaxInactiveInterval() %>" gives values in seconds.

Then why should we use
timeToLeave = <%= (session.getMaxInactiveInterval() * 1000) - 1195000 %>;
secs = parseInt(timeToLeave/1000);

to get the value in seconds...

Sorry, if this seems absurd but I don't know anything about the
<%= (session.getMaxInactiveInterval() %>

arunrs
We SHOULD not.


timeToLeave = <%= (session.getMaxInactiveInterval() * 1000) - 1195000 %>;
secs = parseInt(timeToLeave/1000);

can be written

secs= <%= session.getMaxInactiveInterval()  - 1195 %>;

if you do not need a date object

Michel