Thanks Craig!
 
Craig wrote:
>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)
 
When I tested, I found out that something catastrophic happens when the JSP runs concurrently.  The instance variable declared in the <%!   %>  tag is altered by different threads hence giving a distorted result.  Eg.  Assuming I have <%! private String s = null; %>  and somewhere down my JSP I have:
<%
    s = (String) session.getAttribute("userid"); 
    out.println(s);
%>
 
When this page is executed concurrently from may be 6 different clients; the result will definitely be muddled up - yielding more than one user with the same userid.
 
>I am not sure how you could synchronize it in the jsp code in the service method (ie <%   %>).
 
Well, you can synchronize the JSP code in the <%  %> tag as follows:
<%
synchronized (this){
 
}
%>

This way once a thread enters the block of code, no other thread can enter the same block until the first thread exits.

Someone advised that I avoid using instance variables and I think that's the best way to go.  Though there are cases where my method may need to return more than one value, in which case I could have used instance variables.  Some also suggested I use session.get/set(Attribute) in such case.  Is this the right way to go? 

Thanks

Emma 

 -----Original Message-----
From: Craig Malmfeldt [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 15, 2002 7:29 PM
To: JDJList
Subject: [jdjlist] Re: Thread-Safe JSP

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
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm


Reply via email to