Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > StringBuffer and Synchronization

Reply
Thread Tools

StringBuffer and Synchronization

 
 
Wayne
Guest
Posts: n/a
 
      02-14-2007
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-14-2007
On 14.02.2007 18:02, 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.

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
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-14-2007
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.


Robert Klemme wrote:
> 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).


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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-14-2007
On Feb 14, 2:49 pm, Lew <(E-Mail Removed)> wrote:
> 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.

> Robert Klemme wrote:
> > 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).

>
> 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


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract String and StringBuffer mikew01 Java 9 05-28-2012 07:19 PM
String concatenation versus StringBuffer Darren Java 5 07-28-2004 09:35 AM
hash for String and StringBuffer John Galt Java 4 02-25-2004 11:01 PM
buff = new StringBuffer(10000); buff.toString(); Will GC be complete here? qazmlp Java 5 02-19-2004 04:07 PM
performance string, stringbuffer, and stringtoken Mike mmccaws@netscape.net Java 3 07-07-2003 02:51 PM



Advertisments