• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP declaration instance/static variable doubt

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

A simple counter:
<%! int counter = 0; %>
Page viewed <%= counter %> times.

My doubts are:
1) why doesn't it get reseted at each request?
2) why the counter is the same in different browsers (same machine)?

Thanks in advance!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because there's only one instance of the JSP created, and it's shared across all requests.
 
Han Jie
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leandro.

It may dispel your above 2 doubts after you finding the Servlet source file generated from JSP.

In addition, the scriptlets and javacodes in JSP are already deprecated.

You can use new technologies instead of them completely now.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the same apply to a pure servlet?

I tried and got the same result.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I guess you answered your own question.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Then I guess you answered your own question.

ok. so my question is: is there a difference between instance and static variables when dealing with servlets and JSPs?
 
Han Jie
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Because there's only one instance of the JSP created, and it's shared across all requests.


As david said above, Servlet has the features which are singleton and multi-thread the same as JSP.

This means that there is one and only one instance of it in your own app and all requests access the same one.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. Just like with any Java class, instance variables in the same instance will be retained across multiple method calls.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all. Very helpful.
The answer to my last question: by default, NO.

According to the Servlet specification, a servlet that does not declare SingleThreadModel usually has one and only one instance, shared among all concurrent requests hitting that servlet.

http://www.jguru.com/faq/view.jsp?EID=416009
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!