• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlet/applet communication problem

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic