在Servlet中读取序列化对象时出现EOFException

时间:2011-01-27 14:10:16

标签: java serialization servlets

我有一个小程序试图将java序列化对象发送到servlet(托管在Tomcat 6中)。 URL是正确的,applet到达正确的servlet,但是当它在servlet端读取任何东西时,我都会收到错误。

以下是applet代码:


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

这是servlet代码:


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;

我有一个尝试... catch(IOException ex)当然围绕着两者。错误如下:


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)

我真的不明白这里发生了什么:servlet的ObjectInputStream似乎什么也没得到,但我在同一个web项目中有另一个applet-servlet,虽然有一个http隧道,但它们工作得很好?!

非常感谢任何帮助:)

2 个答案:

答案 0 :(得分:1)

只有在将数据写入ObjectOutputStream后,才尝试在applet中创建ObjectInputStream。在servlet端,延迟创建ObjectOutputStream。

原因:

我认为您编写的代码应该可以正常工作(它的顺序正确),但我知道过去这些流设置的顺序和时间都存在问题。

答案 1 :(得分:1)

在您的servlet上,首先执行所有输入处理,然后才处理输出处理。特别是,我将您的代码重新排序为:

// 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