| Author |
thread in servlet problem
|
michael yue
Ranch Hand
Joined: Nov 20, 2003
Posts: 204
|
|
i need urgent help. I am unable to get the right result when using thread in servlet. The servlet code is import java.io.*; import java.net.*; import java.sql.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.*; public class PMSocServ extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException { doPost(req, res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException { int [] codereturn=null; int val1=11,val2=11; String RO = req.getParameter("routeobj"); String getstr=""; String gila="tex"; try{ Depx DS = new Depx(gila); Thread t = new Thread(DS); t.start(); getstr = DS.getAB(); }catch (Exception e) { System.out.println("Accept failed: 2222"); } PrintWriter out=res.getWriter(); out.println("<HTML>"); out.println("<HEAD>"); out.println("<META name=\"GENERATOR\" content=\"IBM WebSphere Page Designer V3.0.2 for Windows\">"); out.println("<META http-equiv=\"Content-Style-Type\" content=\"text/css\">"); out.println("<TITLE>Result</TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("<H3><FONT SIZE=4><B><U>The Result </U></B></FONT></H3><FONT SIZE=4><B>"); //out.println(" <FONT SIZE=3>Return : "+val1+"::"+val2+"<BR>"); out.println(" <FONT SIZE=3>Return : "+getstr+"<BR>"); out.println("</FONT></B></FONT>"); out.println(" </BODY>"); out.println(" </HTML> "); } //dopost } The runnable class is package PMServlet; import java.io.*; import java.util.*; import java.net.*; public class Depx implements Runnable, Serializable { // private Socket socket; private String merchantnumber="00"; private String ab="start"; private String ax=""; //Constructor public Depx(String mnum) { merchantnumber = mnum; } public synchronized void setAB(String ab){ ax=ab; } public synchronized String getAB(){ return ax; } public void run() { try{ setAB("RUN"); } finally{ setAB("ERR"); } } }//class The DS.getAB() method cant seem to return anything when clearly the setAB method is perform in the run method. Please help
|
 |
Steve Leach
Ranch Hand
Joined: Sep 24, 2003
Posts: 46
|
|
Your getAB() call is immediately after you start the thread, and so the thread won't have started running yet. You need to have your servlet wait for the thread to be ready. Your code doesn't make it obvious why you are using threads though.
|
 |
michael yue
Ranch Hand
Joined: Nov 20, 2003
Posts: 204
|
|
I am just in testing process. I am planning to connect this to another server using socket. Pls how can i stop the problem of waiting for some time. Will putting thread.sleep help. A sample code would ne real helpful please.
|
 |
 |
|
|
subject: thread in servlet problem
|
|
|