Is a variable declared inside an user-defined function in jsp threadsafe?
For example in the below snippet, is the variable 'i' thread safe?
<%! public int increment()
{
int i=0;
return ++i ;
}
Is a variable declared inside an user-defined function in jsp threadsafe?
For example in the below snippet, is the variable 'i' thread safe?
<%! public int increment()
{
int i=0;
return ++i ;
}
Is a variable declared inside an user-defined function in jsp threadsafe?
Yes, it is thread-safe since only the current thread would have access to it. If the method is called by several threads, each thread will use his own local variables for this method.
Further explanation:
Every JSP is compiled into a Servlet and will distribute the writing of the HTML/CSS/JS across several methods (this depends on the application server though). The variables declared in the scriptlets will be inside methods (is not like you can't declare a field that will work cross JSPs), being all of these methods thread-safe. More info: What is the difference between JSF, Servlet and JSP?
Now the problem is, are servlets thread safe? It depends on how you implement them. More info on this: How do servlets work? Instantiation, sessions, shared variables and multithreading. As you can see from the Threadsafety block, in case of the Servlets generated by compiling JSPs, they will always be thread safe because all the variables declared in scriptlets will be inside some method of the generated servlet instead of being declared as a field of the class.
Last but not least, you should not use scriptlets since their use is highly discouraged. You can find further explanation about it here: How to avoid Java code in JSP files?. If you must maintain a legacy system that still uses scriptlets then you're out of luck, at least you can recommend slowly dropping scriptlets usage and move to EL and JSTL.
Your question is meaningless.
A local variable cannot be accessed by multiple threads, so the question of thread safety does not apply.
The instance referred to by a local variable may or may not be visible to multiple threads, and may or may not be thread-safe, depending on how it's designed and used.