Hi,
Following is a very simple servlet code, please help:
public class PrintCheck1 extends HttpServlet implements SingleThreadModel
{
public synchronized void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String transactionID = request.getParameter("transactionID");
if(transactionID == null) transactionID = "";
int total = 0;
response.setContentType("application/pdf");
ByteArrayOutputStream ba = new ByteArrayOutputStream();
if(!transactionID.equals("")){
try {
System.out.println(total);
total ++;
}catch(Exception e){
System.out.println(e.toString());
}finally{
transactionID = "";
}
}//end of if
}//end of doGet
public synchronized void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
System.out.println("get called from doPost");
doGet(request, response);
}//end of doPost
}
And the output of this servlet is:
0
0
0
I think this is because multiple threads are running. But:
1. I am the only client to call this servlet;
2. This class already implements SingleThreadModel;
3. Every variable associated with the request is declard with the doGet method;
4. doGet method is synchronized;
5. If anything is executed in try block, then finally will be called, which set the variable transactionID to empty string.
It seems there are 3 thread running the servlet, why? I think I've done everything needed to control the servlet in a single thread.
Another thing I notices is that: if I comment the following code:
response.setContentType("application/pdf");
then the problem is gone. Is it possible that since it takes some time to open acrobat reader, it generates multiple threads by itself? I call the servlet directly from the location bar of the Internet Explorer.
Anyone has same experience? Thanks in advance.
Lynn
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, and more