Hi!
I have a small servlet that receives some text from a client, and returns a response. The problem is that when i try to receive the data in the servlet, I get and EOFException. And when the client tries to read the response, a StreamCorruptedException is thrown. Can anybody please point me in the right direction here?
The code for servlet is as follows:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DataInputStream in = new DataInputStream((InputStream) request.getInputStream());
String message;
String text = null;
try {
text = in.readUTF();//EOFException thrown here!
message = "100 ok";
} catch (Throwable t) {
message = "200 " + t.toString();
}
response.setContentType("text/html;charset=UTF-8");
response.setContentLength(message.length());
in.close();
PrintWriter out = response.getWriter();
out.println(message);
out.flush();
out.close();
System.out.println(". Received text: " + text == null ? "none" : text);
}
And the code for the client:
@Test
public void test2() throws IOException, ClassNotFoundException {
String input = "This text is to be sent";
URL serverURL = new URL("http://localhost:8080/myURL");
URLConnection con = serverURL.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);//StreamCorruptedException thrown here ("invalid stream header")
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
System.out.println(result);
}
The EOFException is thrown at the line text = in.readUTF(); in the servlet, and the StreamCorruptedException is thrown at ObjectInputStream inputFromServlet = new ObjectInputStream(instr); in the client. What is wrong here?
Edited by: Fush on Apr 15, 2008 6:59 AM
Edited by: Fush on Apr 15, 2008 7:00 AM
Edited by: Fush on Apr 15, 2008 7:03 AM