This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how counter variable works in jsp

 
harish goyal
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish,

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

Access both files.

Compare corresponding Servlets.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • 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.
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic