Emma wrote:

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.
 

Emma,

I am not sure what you are trying to accomplish with what you have posted so far. Yes each individual service method can and will mutate a variable declared within the <%!   %> code blocks. So if you want them to have variables that others can't mutate then declare them within <%  %> blocks which will remain independent of each other and exist within only that user's request to the service method. If, however, you need an incremental userid that each user gets and remains unique for each user but increments sequentially based on when a user accesses the method that is different. I would recommend using an external class ( a java bean) and controlling it there as I mentioned in my previous post. Again that is based on the premise that you need a variable of global scope that each accesses and becomes unique to them like a userid.

Emma wrote:

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.

Emma,
 
I believe that the above code is redundant to what is happening inside the jsp anyway. Each request starts a new thread of execution on the service method. Hence the synchronized block is not needed. You could use a block like that if you called a method within the
<%!     %>  tags. In fact if you called a method within the <%!  %> tags that was synchronized that might give you the  solution you are seeking. Everything within the <%!  %>
tags has global or application scope.
 
Emma wrote:
 

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? 

Emma,
 
If you need a variable with global scope you can not use the request or session objects to track it except for a single user. Each request that comes into your jsp has an individual and unique session (or request) object that would be associated with it. You can however access an application scoped object using :
<% getServletContext().getAttribute("nameOfAttribute") %>
ie <% s = (String)getServletContext().getAttribute("userid");  %>
   <%  getServletContext().setAttribute("userid", <object>);  %>
 
in other words it works the same way that session does except it is an object of application scope.
 
Good luck!

Craig


Chat with friends online, try MSN Messenger: Click Here
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to