JSP variable scoping and Thread safety
Hello:
I'm trying to clarify my understanding of some variable scoping/thread safety issues in my understanding. My question boils down to the following, under what circumstances in a JSP does one need to worry about synchronizing access to avoid potential threading issues for variables? To be more specifiec, I'm trying to determine if there is a correlation between the JSP construct (for example <%! Declaration %>, <% Scriptlet %> and <%= Expression %>) and any variables declared within that contruct and whether it needs to have access synchronized for it? For example, I would assume that any variable declared in the <%! Declaration %> contruct will result in a static variable in the resulting servlet and hence will need to be synchronized to ensure safe access with multiple threads. Is this true? Are there similar rules/guidelines for the other JSP constructs? Of course, I'm assuming more than read only access to said variables for these questions. I'd appreciate any and all assistance/clarification.
Thanks,
Todd
[1087 byte] By [
skidvd] at [2007-9-27 16:21:26]

A look at the source code generated from a JSP would probably answer most of your questions.
<%! A %>
<% B %>
<%= C %>
gives you (more or less)
public class foo_xjsp ...
{
A
public void service(...)
{
B
out.print(C):
}
}
But remember that more than one request can be using a JSP simultaneously (unless you specify the use of the single-thread model).
In almost all cases there is no need to synchronise access to variables declared at B. Access to variables declared at point A (class or instance fields) should be synchronised or inherently thread-safe. Generally, though, fields declared at A are either thread-safe, final or not likely to upset the function of the page.
Hope this helps.
Thanks for your speedy and helpful response. I have a few follow up questions though...
You stated that "In almost all cases there is no need to sunchornise access to variables declared at B." Why is this so? Is it because the B variables are declared as auto variables in the scope of the service(..) method? If the container is responsible for spawning a new Thread for each service(..) invocation, then this would imply that any B variables would reside on that new Thread's stack and hence be thread-safe. Is this the correct understanding? What are the exceptions or few cases that I would need to worry about synchronization?
Also, what if the <% B %> was used to declare a method, for example
<%
public String getContent( ... )
{
// do some magic here to generate content based on
// specified params
}
%>
If the getContent() method is called from another <% B %> or <%= C %> block, would this change any of the above rules/guidelines?
Finally, you did not mention the <%= C %> blocks. I'm assuming that these are Thread safe provided the C is an atomic reference? If C is non-atomic, then I assume that synchronization may be needed (especially if C is an expression involving any <%! A %> values).