Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A tiny JSP page: How is this possible?

 
Mark Lau
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!
 
Madhav Lakkapragada
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
 
Mark Elliott
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
 
HaHa
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic