Emma,
Let me answer your second question first and then try to answer the first. I don't believe anything catastrophic will occur if calls from various service methods are changing the member variable declared with the <%! %> tags, even if they are doing it concurrently. (You would have to test to be sure though) I am not sure how you could synchronize it in the jsp code in the service method (ie <% %>). I would be inclined to not use the <%! %> way of declaring variables but if you wanted a single global variable which various service method threads are updating I would lean towards using a javabean with application scope (essentially making it a Singleton) and use synchronized methods within that bean to update the variable in question. This would create a single point of entry to any updates of the variable and make everything thread safe.
<jsp:useBean id="emmasBean" class="com.bl.br.EmmasBean" scope="application"/>
<%
emmasBean.setS("something else");
%>
where setS(String s) is synchronized in the bean.
You could also use a bean in another scope and declare your variable as static.
You could also declare the jsp not to be thread safe (isThreadSafe="false") but that is not a great solution as the entire jsp essentially becomes synchronized which in essence limits it to one request at a time.
Hope this helps.
Craig
>Thanks Craig.
>
>Assuming that I have this : <%! String s = "whatever"; %><%! String s = "whatever"; %>
>and somewhere in the JSP I have: <% s="something"; %><% s="something" %>
>
>How can I synchronize the second block? What do u think will happen when
>different threads executes the second block?
>
>Thanks.
>Emma
>
>
> -----Original Message-----
> From: Craig Malmfeldt [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 15, 2002 2:29 PM
> To: JDJList
> Subject: [jdjlist] Re: Thread-Safe JSP
>
>
> Emma,
>
> If you are declaring variables within the jsp you need to be careful how
>you are doing it. If for example you declare a variable within the code
>blocks <%! String s = "whatever"; %>
>
> This variable has global application scope for that jsp. Any changes in
>other methods would have to be synchronized or you are not thread safe.
>
> However, if you declare a variable within a jsp within code blocks
>
> <% String s = "whatever"; %>then you need not worry about
>synchronization as this variable is declared within the service method of
>the jsp ( doGet, doPost) and each individual call to the jsp creates a new
>thread for the service method.
>
> Craig
>
>
>
> >Dear All,
> >
> >Is it necessary to synchronize methods in JSP so as to make them thread
> >
> >safe? When do I need to synchronize them? Again, if for instance my
>method
> >
> >need to return multiple values, in which case I'm forced to use instance
> >
> >variables since Java can only return a value. How can I make the JSP
>thread
> >
> >safe?
> >
> >Emma
> >
> >To change your membership options, refer to:
> >http://www.sys-con.com/java/list.cfm
>
>
>----------------------------------------------------------------------------
>--
> Chat with friends online, try MSN Messenger: Click Here
> To change your membership options, refer to:
> http://www.sys-con.com/java/list.cfm
>To change your membership options, refer to:
>http://www.sys-con.com/java/list.cfm
Chat with friends online, try MSN Messenger:
Click Here
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm