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();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…