I have a servlet that gets a ID as parameter and returns three values. Since the servlet code is large ill just give the necessary details.
getdetails.jsp
Inputs : ID //as a query string
Returns: ID, average, count // As a JSON string
From the client side am making two asynchronous javascript requests one by one (second immediately after 1st request) with different IDs. The responses from the server are as follows:
Responses:
For ID1 : ID1, average1, count1
For ID2 : ID1, average2, count2
For ID2 it gives correct average and count but returns the ID1 (ID of 1st request).
When I put the same code in getdetails.jsp in getdetails1.jsp and make each request to each servlet, I get correct results.
Responses:
For ID1 : ID1, average1, count1
For ID2 : ID2, average2, count2
What may be the reason for this and how to correct this?
EDIT:
code of getdetails.jsp:
<%!
String ID;
JsonObject details = new JsonObject(); //using Google JSON Lib
%>
<%
ID=request.getParameter("id");
details.addProperty("ID",ID);
... //accessing corresponding average and count
details.addProperty("average",average);
details.addProperty("count",count);
out.println(details);
%>
The problem is what @thinksteep suggested. JSP code is translated and compiled into a Servlet. Using the JSP declaration
<%! %>creates instance variables that aren’t thread-safe. You should declare the variables so that they are created new for each request by removing them from the<%! %>section and declaring them in the<% %>section.