Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variable in jsp

 
ramya ray
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a variable say 'count' is declared static in jsp and increasing each time...
Will the value of count increase each time the browser access or refresh ...
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramya ray:
If a variable say 'count' is declared static in jsp and increasing each time...
Will the value of count increase each time the browser access or refresh ...


It would help if you provided a code snippet to discuss. If I am reading your mind correctly, I would code your requirements as:


If this is what you meant, then yes the count variable would be incremented on each page hit.

-Bryan
 
ramya ray
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is great, it looks like you are a business maanger
The code snippet you have written depicts my requirement.

I have one doubt - If i press enter on the same browser address then it won't increment but if i refresh the page then it will increment the count ...

Need help for the last paragraph.

Thank,
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to declare int variable count as static.

We will get the same result without static declaration.


<%! private int count = 0; %>
<html>
<body>
<% count++; %>
<p>count=<%=count%></p>
</body>
</html>
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramya ray:
I have one doubt - If i press enter on the same browser address then it won't increment but if i refresh the page then it will increment the count ...


Hmmm... I'm not sure. I think that it depends on your browser... whether it issues an HTTP request when you "press enter". However, I do know that everytime an HTTP request gets processed by this JSP, then yes it will increment. So, your question can be reduced to this: "Will an HTTP request be sent when I press enter on the same browser address?" And that is a question about your browser not about JSPs. Make sense?

-Bryan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic