<%! declaration tag problem to be careful - clarification

Consider 96 for teh percentage sign in the description below throughout

<code>

<%! int i = 5; %>

<%int j=5;%>

<%i++; %>

<%j++; %>

<%out.println(" i value " + i); %>

<%out.println(" j value " + j); %>

</code>

The output of the above program when we run in browser will be

i value 6

j value 6

Now if we refresh the browser the output is

i value 7

j value 6

Then if we open another browser and execute the program the output is

i value 8

j value 6

from above we note that the value of i increases while value of j is always 6.

Here j is a local variable while i is instance variable. But even if we open in new

browser and run the program the value of i always increases.

Actually in the documentation it is given that for declarations we have to use <%! tag

But here when we use that it seems that variable "i" becomes static as the value increases

even if we open in new browsers.

That would cause problem in the general code with does not have isThreadSafe=false

in the page directive.

Like it is very likely that if we do not use isThreadSafe=false and use <%! for declaring

variables then if say person A and person B logs on simultaneously then the value

of i of person A may get printed for person B and if person A refreshs his browser then

the value of i which got incremented for person B may again get incrmented for person A and get

displayed.

I hope many developers use <%! tag for declaring the varaibles do all know that it becomes shareable among

all users of the program if isThreadSafe=false is not used

Is my thinking is correct. Is there a way to say that all jsp pages to have isThreadSafe=false instead

of specifying that in each jsp page in page directive

Thanks to all

[1994 byte] By [mdjavid7] at [2007-9-26 3:02:35]
# 1

Yes, you need to be careful when using <%! declarations.

In your example, there is only one instance of the variable i that is shared across all requests. The variable j, however, is specific for any one request and is protected from other requests.

Even when using <%! declarations, you should synchronize your accesses to the variable since two simultaneous requests could update them at the same time and you could get very unpredictable results (according to how you code your page).

In general, it is better to stay away from <%! unless you need to share state between requests or users.

If you need to share state across requests, I would use the session object instad. For sharing state across users, use the application object or a database.

Hope this helps...

jpardi at 2007-6-29 11:01:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

thanks jpardi one more clarification is if we use application object then is it that synchromizing is not required. Like in your reply it is said that if we use

<!%then we have to ourself take care of synchronizing . Does application takes care of synchronizing itself

regards and thanks>

mdjavid7 at 2007-6-29 11:01:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
One more point i would like to say is that why do books say that <!% is for declaring variables why not highlight the point that whta is declared in ><!% becomes shared between users and not restricted to only requests of a user.thanks>
mdjavid7 at 2007-6-29 11:01:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...