?
Solved

HttpSession related

Posted on 2006-05-12
25
Medium Priority
?
404 Views
Last Modified: 2012-06-27
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() %>

0
Comment
Question by:spiderman19
  • 9
  • 8
  • 7
  • +1
25 Comments
 
LVL 14

Expert Comment

by:boonleng
ID: 16666030
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
0
 

Author Comment

by:spiderman19
ID: 16679487
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?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 800 total points
ID: 16689747
This goes in the head:

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


If you want an update:


<script type="text/javascript">
timeToLeave = <%= (session.getMaxInactiveInterval() * 1000) - 10000 %>;
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>

<span id="show"></span>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Assisted Solution

by:arunrs
arunrs earned 400 total points
ID: 16689752
Hi,

You can try using the following code.

function displayTime()
{
   window.status = "Time till Session Timeout : " +  remainingTime;
}
var remainingTime = parseInt("<%= session.getMaxInactiveInterval()  %>");
function calculateTime()
{
   if (remainingTime > 0)
   {
        window.setTimeout("displayTime()", 1000);
         remainingTime = remainingTime - 1;
   }
}

Call the CalculateTime() in the body onload event. This will display the remaining time for session time out in the status bar of the window.

Hope this is useful.

arunrs
0
 
LVL 5

Expert Comment

by:arunrs
ID: 16689802
sorry the function calculateTime should look like

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

arunrs
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16689808
I still beat you to it ;)
0
 
LVL 5

Expert Comment

by:arunrs
ID: 16689840
Small typo error. It should be

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

arunrs
0
 
LVL 5

Expert Comment

by:arunrs
ID: 16689849
You sure did Michel.:)

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

Author Comment

by:spiderman19
ID: 16705478
ok guys..

let me try each of your ideas and got any problems will come to you again
0
 

Author Comment

by:spiderman19
ID: 16706123
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...

0
 

Author Comment

by:spiderman19
ID: 16706126
i means the window redirecting doesnt work
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16706166
syntax.

if (timeToLeave==0)

Please look here:
http://www.w3schools.com/js/default.asp
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16706183
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>
0
 

Author Comment

by:spiderman19
ID: 16706857
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.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16706944
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
0
 

Author Comment

by:spiderman19
ID: 16707041
ok.. my mistake.

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

Expert Comment

by:arunrs
ID: 16707075
Hi,

I have a doubt...
Is it

if (timeToLeave==0) or is it

if (secs==0)  ??

arunrs
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16707088
And I am sorry I did not pay attention:

  if (secs<=0) {

not

if (timeToLeave==0) {
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16707093
Better:

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

Author Comment

by:spiderman19
ID: 16707221
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?
0
 
LVL 5

Expert Comment

by:arunrs
ID: 16707767
Hi

If that is the case..
then use

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

arunrs
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16707859
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 ....
0
 

Author Comment

by:spiderman19
ID: 16714416
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.
0
 
LVL 5

Expert Comment

by:arunrs
ID: 16715227
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
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16715437
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If anyone asked you to network diagram of the internet, it was drawn in the form of a fluffy cloud which further became known as cloud computing. Popularly cloud computing is defined as workloads that run over the internet in a commercial provider’s…
Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question