Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

StringBuffer and Synchronization

Thread view: 
Wayne - 14 Feb 2007 17:02 GMT
I have a JSP that has a StringBuffer that is appended to 30 times and
at the end it is converted to a string with sb.toString().  The
problem I have with two almost instantanious executions of the JSP the
contents of the StringBuffer are mixed.  I thought that StringBuffer
was thread safe.  Any suggestions would help.

Thanks in advance
Robert Klemme - 14 Feb 2007 17:06 GMT
> I have a JSP that has a StringBuffer that is appended to 30 times and
> at the end it is converted to a string with sb.toString().  The
> problem I have with two almost instantanious executions of the JSP the
> contents of the StringBuffer are mixed.  I thought that StringBuffer
> was thread safe.  Any suggestions would help.

Well, content is mixed - but in a thread safe manner. :-)

Seriously, you probably have made the StringBuffer an instance variable
of the JSP.  Consequently all invocations share the same instance (note
that a single JSP by default can be invoked multiple times
*concurrently*).  You need to change it from an instance variable to a
local variable (i.e. created for every execution).

Regards

    robert
Lew - 14 Feb 2007 22:49 GMT
Wayne wrote:
>> I have a JSP that has a StringBuffer that is appended to 30 times and
>> at the end it is converted to a string with sb.toString().  The
>> problem I have with two almost instantanious executions of the JSP the
>> contents of the StringBuffer are mixed.  I thought that StringBuffer
>> was thread safe.  Any suggestions would help.

> Well, content is mixed - but in a thread safe manner. :-)
>
[quoted text clipped - 3 lines]
> *concurrently*).  You need to change it from an instance variable to a
> local variable (i.e. created for every execution).

Even better, don't declare StringBuffers in JSPs at all. There is danger in
having to much Java scriptlet in a JSP; some would say none is the maximum.

(Marty Hall, author of / Core Servlets & JavaServer Pages/, is an infamous
excoriator of scriptlet in JSP.)

- Lew
Daniel Pitts - 14 Feb 2007 22:52 GMT
> Wayne wrote:
> >> I have a JSP that has a StringBuffer that is appended to 30 times and
[quoted text clipped - 17 lines]
>
> - Lew

Where I work (won't name names, but its a large media company), our
"legacy" JSPs have too much Java mixed in.

We are moving away from our legacy system, and our newer webapps don't
allow ANY java in JSPs.  For the record, we use Spring MVC.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.