aspose file tools*
The moose likes JSP and the fly likes A tiny JSP page: How is this possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "A tiny JSP page: How is this possible?" Watch "A tiny JSP page: How is this possible?" New topic
Author

A tiny JSP page: How is this possible?

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
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

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Mark Elliott
Greenhorn

Joined: Jan 15, 2002
Posts: 6
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

Joined: Jan 16, 2002
Posts: 4
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A tiny JSP page: How is this possible?