Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

A tiny JSP page: How is this possible?

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this:
<HTML>
<HEAD>
<TITLE>JSP Declarations</TITLE>
</HEAD>
<BODY>
<H1>JSP Declarations</H1>
<%! private int accessCount = 0; %>
<H2>Accesses to page since server reboot:
<%= ++accessCount %></H2>
</BODY>
</HTML>
What this JSP page does is just keep counting the number of hits of this current page via the variable "accessCount", which does not seem to be a predefined variable.
But I don't understand the mechanism. What triggered the execution of <%= ++accessCount %>? I think, prior to this statement, we should put
something like:
"if the refresh button (or reload button) is pressed or if the browser if closed and reopened"
Otherwise, how does "accessCount" get incremented each time the page is reloaded?
Now, if you say that the JSP engine does the dirty work behind the scene, then that will result in every statement such as "++someVariable" being incremented simply because the webpage is reloaded. Consider:
if ( isItemOrdered() )
{ ++numOfItemsOrdered;
}
We certainly don't want the "numOfItemsOrdered" to get incremented each time the page is reloaded, do we?!
So, back to "accessCount", how does it get incremented? It behaves as if it were an actionListerner of the refresh button!
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if ( isItemOrdered() )
{ ++numOfItemsOrdered;
}
We certainly don't want the "numOfItemsOrdered" to get incremented each time the page is reloaded, do we?!

Thats the reason why you have an if stmt followed by a method call to isItemOrdered(). This method is expected to have the business logic that gets data from the HTTP request and returns a true/false based on which your if stmt is changes the logic.
A lot of the doubts you have will be dealt with in a fundamental JSP book or tutorial. Checkout some tutorials and you will understand.
Good luck.
- satya
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason that your variable is being updated every time is that by using the ! notation you have declared it as an instance variable of your jsp page. The instance of this page will stay alive as it were, for the entire time that the server is up, the variable will be incremented by every access of this page (which will include reloads). Due to the fact that every request will access the same instance of the page.
I think I have mixed up some terms here but I hope you get my meaning
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to use this counter, but it won't work. Can someone show me how to add a counter (without using cgi?) on a html page? Thanks very much,
Haha
 
    Bookmark Topic Watch Topic
  • New Topic