• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

page request counter

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am new to JSP.
I dont know what's wrong with the code.
First time when we load the page it says:
"This page has been Visited 1 times"
next time also it says the same.
from third time onwards it work.
What is that?

<HTML>
<%! int i =0; %>
<%

i=i+1;
out.println("This page has been Visited "+i+" times");

%>

</HTML>
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it consistently do that? 1st is ok, 2nd bad, 3rd and rest ok?

Try restarting your server and try. Do this 3 or 4 times and let us know if all those tries with restarting server, gives same result.
 
Jesus Angeles
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take note that it is your container's decision to reuse a new servlet instance (jsp is compiled into a servlet), or instantiate a new servlet.

For that reason, your counter can go back to 1 anytime. It is practically unpredictable.

Just to play with it (this is a bad design thing in most cases), make that 'i' as static. That will fix your issue, but as I mentioned above, you most likely wont use static in enterprise applications as it bring a lot of issues like synchronization.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic