This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reloading issue

 
Alex George
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do have the following code in the header jsp. The idea is that I want to collect the visitor time using javascript and
set it into session so the cotroller part of the application can call an appropriate method to use this.
But the fact is that when I monitor the execution in the console window, I see it getting executed many times and
it wouldn't stop executing. It keep submitting the form again and again and wouldn't stop.
Anybody can think of some other way to make it work?


<head>
<SCRIPT LANGUAGE="JavaScript">
function fnSubmit() {
window.document.form1.submit();
return;
}
</SCRIPT>
</head>

<body onloa="return fnSubmit()">
<form name="form1">
<script language="JavaScript">
<!--
var visitortime = new Date();
document.write('<input type="hidden" name="VisitorTime" ');
if(visitortime) {
document.write('value="' + visitortime + '"> ');
}
else {
document.write('value="JavaScript not Date() enabled">');
}// -->
</script>
<noscript><input type="hidden"
name="VisitorTime" value="Browser not JavaScript enabled">
</noscript>

</form>
<%
String VisitorTime = request.getParameter("VisitorTime");


System.out.println("####################################");
System.out.println("### Visitor Time is " + VisitorTime + "###");
System.out.println("####################################");
%>

</body>


Thanks in advance.
[ December 10, 2007: Message edited by: Alex George ]
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you keep loading the same page. Onload is being fired everytime since that is what it does. You would need to make the page smart [don't output code] or have the page submit to a different location.

Eric
 
Alex George
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I make it smart. I output the code just to see if the value is getting set in the session.
I highly appreciate your help.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Have your server output the code when you get the value back you do not display it again.
  • You add a cookie that determines if you need to post back [if cookies are disabled the user will be stuck in a loop.]
  • You post the page to another page where the code does not exist.
  • You just wait to the user posts back the page to see the value.

  • Eric
     
    Alex George
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    header is getting inserted in all the pages. So I cannot really submit this anywhere.
     
    Eric Pascarello
    author
    Rancher
    Posts: 15385
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Than you need to say hey I go this already, do not display it.

    Eric
     
    Paul Clapham
    Sheriff
    Posts: 21319
    32
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Alex George:
    The idea is that I want to collect the visitor time using javascript and
    set it into session so the cotroller part of the application can call an appropriate method to use this.
    Maybe I am missing something, but wouldn't it be easier to just collect the visitor time directly in that JSP, rather than generating HTML that sends a second request?
     
    Alex George
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What Mr.Eric Pascarello said worked for me.
    But how can I get the visitor time in the jsp without using javascript?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic