Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread safety in JSP

 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Bhupinder Dhillon
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I say isThreadSafe=true,
just to be precise, thats the default value anyways!
- satya
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic