Reputation: 649
I have an applet trying to send java serialized object to a servlet (hosted in Tomcat 6). The URLs are correct and the applet reaches the correct servlet, but when it comes to reading anything on the servlet side, I get an error.
Here is the applet code :
String adresse = "/Applic_ClientsPartners/NewMemberServlet";
URL page = getDocumentBase();
String protocole = page.getProtocol();
String host = page.getHost();
int port = page.getPort();
System.out.println(protocole + "://" + host + ":" + port + adresse);
URL serv = new URL(protocole, host, port, adresse);
URLConnection conn = serv.openConnection();
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
//conn.setRequestProperty("Content-Type", "application/x-java-serialized-object");
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.setDoInput(true);
conn.setDoOutput(true);
System.out.println("Connection open");
ObjectOutputStream oos = new ObjectOutputStream(conn.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());
System.out.println("Envoi de 'test'"); //sending a test String
String t = "test";
oos.writeObject(t);
System.out.println("test flush");
oos.flush();
System.out.println("test between flush & writeObject");
oos.writeObject(nouveau); //this is a serializable custom object
oos.flush();
//Boolean result = ois.readBoolean();
ois.close();
oos.close();
System.out.println("Streams fermés"); //streams closed successfully
Here is the servlet code :
response.setContentType("application/octet-stream");
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
System.out.println("AVAILABLE " + ois.available());
System.out.println("Pré read test");
String t = (String) ois.readObject();
System.out.println(t + " lu");
Cards.Member nouveau = (Cards.Member) ois.readObject();
System.out.println("Post read test");
oos.close();
ois.close();
System.out.println("Streams fermés"); //Streams closed successfully
return;
I have a try ... catch(IOException ex) surrounding both of course. The error is the following :
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2554)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at Servlets.NewMemberServlet.doPost(NewMemberServlet.java:96)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
I really don't understand what's happening here : The servlet's ObjectInputStream seems to get nothing, but I have another applet-servlet in the same web project, albeit with an http tunnel, and they're working perfectly?!
Any help would be greatly appreciated :)
Upvotes: 0
Views: 4478
Reputation: 2682
On your servlet, do all input processing first, and only then deal with the output processing. In particular, I'd reorder your code as:
// Read input:
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
System.out.println("AVAILABLE " + ois.available());
System.out.println("Pré read test");
String t = (String) ois.readObject();
System.out.println(t + " lu");
Cards.Member nouveau = (Cards.Member) ois.readObject();
System.out.println("Post read test");
ois.close();
// Write output:
response.setContentType("application/octet-stream");
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
// oos.writeObject(someSerializablebject);
oos.close();
System.out.println("Streams fermés"); //Streams closed successfully
Upvotes: 1
Reputation: 17877
Try creating the ObjectInputStream in your applet only after you have written data to the ObjectOutputStream. And on servlet side, delay creation of the ObjectOutputStream.
Reasons:
I think the code as you have written it should work (it's in correct sequence), but I know I've had issues around sequence and timing of these stream setups in the past.
Upvotes: 1