• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet/applet communication problem

 
Baijun Jiang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I encountered this problem today and looking for an answer.
I have an applet which can query a servlet and get an object back from that servlet.
Here is the codes:
Applet:
URL urlServlet = new URL("...");
URLConnection con = urlServlet.openConnection();

con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
...
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject("");
oos.flush();
oos.close();

// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
Object result = inputFromServlet.readObject();
inputFromServlet.close();
instr.close();

servlet:
res.setContentType("application/x-java-serialized-object");

// read an object from applet
InputStream in = req.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String temp = (String) inputFromApplet.readObject();

// send back an obect to the applet
OutputStream outstr = res.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(returned);//should change to returned
oos.flush();
oos.close();

However, when I run this applet I got an error message:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at applet.trend.trendchart.getData(trendchart.java:238)
...

Can anyone help me to figure out what went wrong?
Thanks.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just two wild guesses: Is the object you send to the applet null? Do you have the same version of the object class file on both sides?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Remove the oos.close() - that is causing the connection to close so the InputStream cant be read. The call to flush() is all you need.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic