-1

I am trying to send String to server(running on tomcat) and have it return the String. The client sends the string, the server recieves it, but when the client gets it back the String is null.

doGet() should set String in = input from client. But doPost() is sending String in = null.

Why? I would assume that doGet() runs before doPost() because it is being called first by the client.

Server:

private String in = null;

public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException{
    try{
    ServletInputStream is = request.getInputStream();
    ObjectInputStream ois = new ObjectInputStream(is);

    in = (String)ois.readObject();

    is.close();
    ois.close();
    }catch(Exception e){

    }
}

public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException{
    try{
    ServletOutputStream os = response.getOutputStream(); 
    ObjectOutputStream oos = new ObjectOutputStream(os); 

    oos.writeObject(in);
    oos.flush();

    os.close();
    oos.close();
    }catch(Exception e){

    }
}

Client:

URLConnection c = new URL("***********").openConnection();
c.setDoInput(true);
c.setDoOutput(true);

OutputStream os = c.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);

oos.writeObject("This is the send");
oos.flush();

InputStream is = c.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
System.out.println("return: "+ois.readObject());

ois.close();
is.close();
oos.close();
os.close();
JJJ
  • 31,545
  • 20
  • 84
  • 99
CptRayMar
  • 133
  • 3
  • 19
  • 2
    `doPost` doesn't run at *all* unless you actually post something. And I'm a little confused; what specifically are you *trying* to do? If you're writing a servlet, use HTTP connections. If you want to shuffle objects back and forth, open a socket. – Dave Newton Aug 10 '13 at 13:19
  • is doPost not getting called when client asks for InputStream? – CptRayMar Aug 10 '13 at 13:20
  • I am only testing the server at this stage. I want to send the server sql in the form of a string, and have the server talk to the database and then return the result. – CptRayMar Aug 10 '13 at 13:22
  • Then use HTTP and make a real HTTP request, e.g., with HttpClient. – Dave Newton Aug 10 '13 at 13:35
  • Stop right there: you are using an instance-scoped reference (`in`) and so you have a serious multi-threading problem in your servlet. If you deploy this into any non-toy setting, mass confusion will occur. You should be using a local reference (e.g. `in` is declared inside of the doGet or doPost method). – Christopher Schultz Aug 10 '13 at 13:37
  • Also, don't ignore exceptions like you're doing. You're shooting yourself in the foot by hiding all the errors that could happen. – JB Nizet Aug 10 '13 at 13:38
  • This code makes no utter sense. As to the client side, this may be helpful: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests As to the data transfer format, serialization is a very poor choice. Better use XML or JSON. There are libraries to convert between Java objects and XML or JSON in an oneliner. – BalusC Aug 10 '13 at 19:04

1 Answers1

0

If you want to read an arbitrary String from a client (or send it back), then you want to just read and write the String directly: there is no need to use ObjectInputStream and ObjectOutputStream. Like this:

public void doPost(...) {
  BufferedReader in = new BufferedReader(request.getReader());
  String s = in.readline();
  ...
}

If you want to be able to echo the string back to a client (but also protect the data from others), then you should use an HttpSession. If this is supposed to be some kind of "echo" service where you want any client to be able to set the string value and then all clients get the same one back, then you should not use HttpSession and instead use an instance-scoped reference as you have above.

Christopher Schultz
  • 18,184
  • 5
  • 54
  • 70