The solution given here will definately work but probably we don't need to
do so much for just a variable.
All the variables defined in the <%! %> tag become the private member
variables of the servlet so they are shared accross all the request threads.
But the variables defined with in the service method of the servlet are not
shared between threads as this method is executed a fresh for every request.
All the variables defined within the <% %> tags in the JSP become the member
variables of the service method and you will always get value as 0 for every
request. Ofcourse , if you want the value to be persistant over a session ,
you will have to put it in a session variable. But while extracting it back
from the session variable , be careful to extract it into a member variable
of the service method of the servlet.
chao
-Abhi
-----Original Message-----
From: Hans Bergsten <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Monday, January 24, 2000 3:26 AM
Subject: Re: important for all jsp developers
>Ashwani Kalra wrote:
>>
>> Hi all ,
>> Can any body provide some information for the following situation ?
>>
>> Since JSP page is compiled into the servlet class that has only one
>> instance(normally) and initilized only once. All the variables declared
>> using <%! %> tags become the part of the member variables of the servlet
>> class(i.e. page implementation class).
>>
>> Now consider follwing scenario.
>> I have declared one int variable and initilized to 0. My first http
request
>> to this variable sets to some other value say 100.Now my question is that
if
>> i make another request to same page and display this variable . What will
be
>> ouptput 0 or 100.(looks that it should be 100 but this will create
problem
>> for me. because my second request is expecting 0 value to be used in
>> calculation.)
>> can any Body suggest a solution for this so that each page will have its
own
>> value.
>> eagerly waiting for reply!!!!!!!!
>
>My general advice is, don't use <%! %> at all. It creates exactly the
multi-threading
>problem you describe.
>
>If the value is only needed during processing of one request, and depending
on how
>the value is used, use scriptlets to declare the variable as a local
variable
>or beans with page or request scope.
>
>If the value must be available across requests, use session scope, or
application
>scope if it must also be available to other clients. But with session and
>application scope you need to consider multi-threading issues again.
>
>Hans
>--
>Hans Bergsten [EMAIL PROTECTED]
>Gefion Software http://www.gefionsoftware.com
>
>===========================================================================
>To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
>FAQs on JSP can be found at:
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html