• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

how counter variable works in jsp

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<html>
<body>
<%! int i =1; %>
<%= i++ %>
</html>
</body>

output: 1 (at every refresh value is increasing)

________________________________________________________

<html>
<body>
<% int i =1; %>
<%= i++ %>
</html>
</body>


output: 1 (constant value not changing on refresh)

can anyone explain this behavior
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harish,

Just make two different jsp files for different type of declarations.

Access both files.

Compare corresponding Servlets.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What ever you write in Scriplet goesinto _jspService method which means they are local variables.

Similarly those written in Declarations are part of instance variables & methods (means outside service method) in the compiled Servlet of your JSP Page.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This declares an instance variable 'i' in the generated servlet. This variable is common to all requests that hit this servlet and hence the page count is increased.


This declares a variable 'i' inside _jspService method that is run in a separate Thread for every request. So this variable is initialized to 1 for every request and printed as 1.

Please note variables declared inside <%! .. %> are not thread-safe.
[ October 04, 2005: Message edited by: Sravan Kumar ]
 
harish goyal
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<%! int i =1; %>

This declares an instance variable 'i' in the generated servlet.
This variable is common to all requests that hit this servlet
and hence the page count is increased.

--------------------------------------------------
so in jsp we can consider instance variable as static variable
which is common for all requests
and
<%! int i =1; %> is not thread safe
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It is not static unless you declare it so. It is an instance variable. Since there will be only one instance of a servlet and that will be accessed by all threads that process a request to this servlet, it is not thread-safe.

Whereas, a variable declared in <% .. %> [scriptlet] lands up inside the _jspService method and hence every thread has its own copy. So, it is thread-safe.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It behaves a lot like a static variable because there will be only one instance of the generated servlet per JVM.
You are correct, instance variables are not thread safe.
 
harish goyal
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sravan Kumar:
[QB]

It is not static unless you declare it so. It is an instance variable.

--------------------------------------------
so if we declare it as a static variable
what will happen
what will be the effect

 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no effect in a servlet environment because there will be only one instance of a servlet per JVM. But, this variable will not be bound to this instance, but to the Class.
 
harish goyal
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if we use single thread model
than we can use static variable
am i right
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. You should not use SingleThreadModel. It doesn't solve thread related problems like it appears to, and has been deprecated.
2. There is no need of declaring the counter variable static. It works without it.

If you use SingleThreadModel, different instances of the Servlet are created / used for different requests and in that case, to update the number of Servlet hits, you need to declare the counter variable as static. But, the bible says not to use it !!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by harish goyal:
if we use single thread model
than we can use static variable
am i right



That is one of the big differences between declaring an instance static and not. Even with SingleThreadModel, instance variables declared static will be shared among servlets.
 
Tell me how it all turns out. Here is a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic