I have this jsp client that consumes a webservice. The problem with the client is whenever it calls the webservice and retrieves the result, it appends the result to the previous call’s result and displays it. But, if i redeploy the war file, the result appears fine. (only for the first time though)
Here’s the code without the import statements.
<html>
<body>
<%! public static Reader fr; %>
<%! public static StringBuffer sb; %>
<%! private static final int BLKSIZ = 8192; %>
<%! public static String file, output; %>
<%
FileparserService service = new FileparserService();
Fileparser port = service.getFileparserPort();
sb = new StringBuffer();
char[] b = new char[BLKSIZ];
int n;
try {
fr = new FileReader(<file>);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while ((n = fr.read(b)) > 0)
sb.append(b, 0, n);
file = sb.toString();
output = port.getRanks(file);
%>
The final output is <br/>
<%out.print(output); %>
</body>
<% fr.close(); %>
</html>
Thanks,
Deepak.
First:
This is bad practice. It belongs in a real Java Class. JSP is a view technology providing a template for HTML/CSS/JS and the ability control page flow and interact with backend Java code using taglibs and access backend data using EL. This particular code needs to be done (in)directly in a class which
extends HttpServlet.Back to your problem: although you declared static variables in a JSP page (of which there’s only one instance during webapp’s lifetime!!), you reinstantiate them all on every request. The obvious thread safety issues aside, the root cause of your problem is not in the posted code, in spite of the static variables. The only thing which look suspicious is the following line:
Doesn’t the
getRanks()method do something with a static variable of theFileParserclass? The chance is big when I look at the coding style/approach of the JSP. Maybe the method is appending the value to a static variable everytime without reinstantiating it (or better, without just declaring it threadlocal inside the method block) and then returning it? That would explain the problem you’re facing.