Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is JSP thread safe?

 
gopi nath
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have an entry screen which runs into several pages and
I have an object containing all data which is put into session . At the last page only i insert into database using a EJBean method.
I am using JSP, EJB and Weblogic.
The problem i have faced sometimes is:
Sometimes the values in session pass onto diff users.Thats why i have asked is JSP thread safe? And do we need to write some special code to make it thread safe?
Thanks in advance
Gopinath
 
Sarath Mohan
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just go through the <%@ page directiv attributes.
there is an attribute called isThreadSafe.
I think this suffices your requirement
Binu
 
Ash Rai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is it not true that by making it thread safe we can actually slow down the access time for different requests?? I am facing a similar problem...I am using jsp,servlets and java beans for getting information from the user and then populating the database with those. I have used the isThreadsafe=true option. Will my application face a problem like Gopitnath's? If I want to use synchronise keyword instead then which are the code statements that need to be placed in the synchronised block? Is it sufficient to synchronise just the psmt.executeQuery() and executeUpdate() methods only or should I synchronise even the java beans (becoz their get and set methods may be used by multiple users at the same time)?
Please help
Thanks
 
sathish jayaraman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
U have to make the setter and getter methods of the bean also synchronized for threadsafety.
sathish.

Originally posted by Ash Rai:
Hi,
Is it not true that by making it thread safe we can actually slow down the access time for different requests?? I am facing a similar problem...I am using jsp,servlets and java beans for getting information from the user and then populating the database with those. I have used the isThreadsafe=true option. Will my application face a problem like Gopitnath's? If I want to use synchronise keyword instead then which are the code statements that need to be placed in the synchronised block? Is it sufficient to synchronise just the psmt.executeQuery() and executeUpdate() methods only or should I synchronise even the java beans (becoz their get and set methods may be used by multiple users at the same time)?
Please help
Thanks

 
lokesh reddy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gopi,
The jsp page by default is not thread safe, if u want to make it thread safe then you have add the following directive in your jsp page declaration.
<%@ page language="java" isThreadSafe=false%>
Dont think i have put isThreadSafe=false by mistake. You have to make it false, if u want to make ur jsp page thread safe. Bye default the attribute will be true.
If u want to find whats the reason behind it, then i would like to suggest you to go through the jsp specification.
I hope u find this info useful.
Bye.
Loke.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by lokesh reddy:
The jsp page by default is not thread safe, if u want to make it thread safe then you have add [isThreadSafe=false] in your jsp page declaration.

if u want to make it thread safe then you have add [isThreadSafe=false]... it sure looks looks odd when you put it that way
Whether your JSP is treadsafe or not is a consequence of the way you implemented your JSP. Strictly speaking you cannot change that simply by modifying an attribute. JSPs are normally perfectly threadsafe, since your run of the mill JSP does not hold any state (member variables). Generally only when you introduce member variables into the JSP object does it become thread-unsafe.
If your JSP is not threadsafe you will have to add the isThreadSafe="false" (note quotes) in order for things to work correctly. I'm probably a bit pedantic when I say that it does not make your JSP threadsafe but it does make your web application threadsafe by instructing the servlet container to cater for the thread-unsafety of your JSP.
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic