StringBuffer and Synchronization

Discussion in 'Java' started by Wayne, Feb 14, 2007.

  1. Wayne

    Wayne Guest

    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
     
    Wayne, Feb 14, 2007
    #1
    1. Advertising

  2. 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
     
    Robert Klemme, Feb 14, 2007
    #2
    1. Advertising

  3. Wayne

    Lew Guest

    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
     
    Lew, Feb 14, 2007
    #3
  4. Wayne

    Daniel Pitts Guest

    On Feb 14, 2:49 pm, Lew <> 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.
     
    Daniel Pitts, Feb 14, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    3
    Views:
    1,872
  2. qazmlp
    Replies:
    5
    Views:
    758
    Chris Smith
    Feb 19, 2004
  3. John Galt

    hash for String and StringBuffer

    John Galt, Feb 24, 2004, in forum: Java
    Replies:
    4
    Views:
    570
    Doug Pardee
    Feb 25, 2004
  4. Darren
    Replies:
    5
    Views:
    4,519
    Darren
    Jul 28, 2004
  5. mikew01
    Replies:
    9
    Views:
    346
    markspace
    May 28, 2012
Loading...

Share This Page