wood burning stoves 2.0*
The moose likes Servlets and the fly likes Thread safety in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread safety in JSP" Watch "Thread safety in JSP" New topic
Author

Thread safety in JSP

Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
In the page directive,we have isThreadSafe attribute for Thread Safety.If I say isThreadSafe=true, i.e. if I donot want to implement the SingleThreadModel, is Thread safety taken care of automatically.
For instance can I use declaratives (<%!..%> ), without concerning about Thread safety?I was under the impression that if I declare variables using the declaration tag, it would not be thread-safe!!
Assuming that, I was always using scriplets (<% %> ), so that all my delarations are local to the Thread!!
Please clarify!
Thanks in advance,
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56

  5. [This message has been edited by Desai Sandeep (edited April 25, 2001).]


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by Desai Sandeep:
Hi,
In the page directive,we have isThreadSafe attribute for Thread Safety.If I say isThreadSafe=true, i.e. if I donot want to implement the SingleThreadModel, is Thread safety taken care of automatically.

No.

For instance can I use declaratives (<%!..%> ), without concerning about Thread safety?I was under the impression that if I declare variables using the declaration tag, it would not be thread-safe!!

You are right, it won't be thread safe if you use the declaration tag. The only thing jsp container does with isThreadSafe flag is to determine whether or not to implement the SingleThreadModel at compile time. If you are telling the container that it's a thread safe jsp then all the synchronization issues should be scrutinized just like any other java app. Also note that if your jsp is implementing SingleThreadModel, you would still have to synchronize the shared resources such as file on the server.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
If I say isThreadSafe=true,
just to be precise, thats the default value anyways!
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Bhupinder,
Thank you for the response.
So, just like we handle Thread safety in servlets, we do the same in JSP.Declaratives being instance variables declared in class scope are not thread-safe.Hence, declaring them using scriplets puts them in the local scope and hence makes it Thread safe....Right?
Also, sometimes we do have a requirement of instantiating many such instance variables.I normally tend to define a inner class and instantiate it in one go in my service method.If the same has to be done in JSP, do we need to define the inner class in the scriplet tag?

Also note that if your jsp is implementing SingleThreadModel, you would still have to synchronize the shared resources such as file on the server.

Yes,that is right!!The shared resources has still to be synchornized for complete Thread safety
Thanks,




------------------
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by Desai Sandeep:

So, just like we handle Thread safety in servlets, we do the same in JSP.Declaratives being instance variables declared in class scope are not thread-safe.Hence, declaring them using scriplets puts them in the local scope and hence makes it Thread safe....Right?

You got it and if any doubts you can always check the generated class file. It would show all your declared variables as instance variable.

Also, sometimes we do have a requirement of instantiating many such instance variables.I normally tend to define a inner class and instantiate it in one go in my service method.If the same has to be done in JSP, do we need to define the inner class in the scriplet tag?

I don't see how you would declare an inner class, plus it would defeat the purpose of JSPs.
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
isThreadSafe is a promise you make to the JSP container, not a promise it makes to you. You should not use non-readonly instance variables anyway - synchronized or not. The inner class technique you describe is a good idea - I use it all the time. You define the class itself in a declaration:

but don't declare an instance of it in the declaration. Do that inside your scriptlet:

------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
Website: http://www.philhanna.com
[This message has been edited by Phil Hanna (edited April 25, 2001).]


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Thank you Phil and Bhupinder for your response.
Regards,

------------------
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread safety in JSP