• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is this happening when i refresh the page ?  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My jsp prints 0 to 4 correctly for the first time. But when I go to the previous page and "call" this jsp again i dont see any numbers. Why ?

The jsp :


 
H Light
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
strange problem!

print 'code' variable

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Light wrote:strange problem!
print 'code' variable


 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently you have 'count' in a declarations scriptlet block.
That makes it an attribute of the servlet.
The standard from the Servlet spec is for one instance of a servlet to be instantiated, and then re-used with each incoming request.
Therefore the value of 'count' is retained between calls, because it is tied to the servlet itself, and not to any particular request.

What happens when you change the line



to




 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
What happens when you change the line


to



The 5 numbers remain on the page.

 
Stefan Evans
Bartender
Posts: 1834
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does that answer your question then?

In the original the numbers don't get printed, because the value of count is already 5, and thus it never enters into the loop on subsequent visits to the page.

Declaring the variable locally, effectively resets it to 0 on each request, and thus the loop executes each time.

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, its clear now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!