File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes thread in servlet problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "thread in servlet problem" Watch "thread in servlet problem" New topic

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.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, {
doPost(req, res);
public void doPost(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, {
int [] codereturn=null;
int val1=11,val2=11;

String RO = req.getParameter("routeobj");
String getstr="";
String gila="tex";

Depx DS = new Depx(gila);
Thread t = new Thread(DS);
getstr = DS.getAB();
}catch (Exception e) {
System.out.println("Accept failed: 2222");

PrintWriter out=res.getWriter();
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("<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(" </BODY>");
out.println(" </HTML> ");
} //dopost

The runnable class is
package PMServlet;
import java.util.*;
public class Depx implements Runnable, Serializable {
// private Socket socket;
private String merchantnumber="00";
private String ab="start";
private String ax="";
public Depx(String mnum) {
merchantnumber = mnum;

public synchronized void setAB(String ab){


public synchronized String getAB(){

return ax;

public void run()


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.
I agree. Here's the link:
subject: thread in servlet problem
It's not a secret anymore!