wood burning stoves*
The moose likes JSP and the fly likes how counter  variable  works in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how counter  variable  works in jsp" Watch "how counter  variable  works in jsp" New topic
Author

how counter variable works in jsp

harish goyal
Ranch Hand

Joined: Aug 28, 2005
Posts: 51
<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

Joined: Sep 16, 2004
Posts: 3636
Harish,

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

Access both files.

Compare corresponding Servlets.


My blood is tested +ve for Java.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121

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 ]

keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
harish goyal
Ranch Hand

Joined: Aug 28, 2005
Posts: 51

<%! 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

Joined: Sep 11, 2005
Posts: 121


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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
harish goyal
Ranch Hand

Joined: Aug 28, 2005
Posts: 51

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

Joined: Sep 11, 2005
Posts: 121
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

Joined: Aug 28, 2005
Posts: 51
if we use single thread model
than we can use static variable
am i right
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
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

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how counter variable works in jsp