File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Distributed Java and the fly likes servlet/applet communication problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "servlet/applet communication problem" Watch "servlet/applet communication problem" New topic
Author

servlet/applet communication problem

Baijun Jiang
Greenhorn

Joined: Oct 13, 2005
Posts: 17
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.


SCEA, CISA.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41865
    
  63
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?


Ping & DNS - my free Android networking tools app
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet/applet communication problem