This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Servlet to Applet Communcation  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing an application that uses an applet for the user interface and a servlet for data retrieval. I am having an issue getting the data from the Servlet back to the applet. My control flow is below:

1. The applet creates an serialized object that is sent to the servlet using the ObjectOutputStream. - This works fine.
2. The servlet receives the object via the ObjectInputStream and pulls date from the object to create a database query. - Also works fine.
3. Using the data from the query, the servlet builds a serialized object that it sends back to the Servlet via the ObjectOutputStream. - Seems to work.
4. The applet then reads the object sent via the ObjectInputStream, but the object is empty.

Just before sending the object from the servlet back to the applet, I print out the objects get methods and the data is in the object.
I do the same when the applet recieves the object from the servlet, but there is no data.

This is the code I am using to return the object (myPlayer) from the Communicator servlet to the applet:

System.out.println("Communicator ==>> getArmorClass = " + myPlayer.getArmorClass()); - The data displayed is correct.
System.out.println("Communicator ==>> getCharName = " + myPlayer.getCharName()); - The data displayed is correct.


res.setContentType("java-internal/" + Player.class.getName());
OutputStream out = res.getOutputStream();

ObjectOutputStream dataOutput = new ObjectOutputStream(out);
dataOutput.writeObject(myPlayer);
dataOutput.flush();
out.close();
dataOutput.close();

In the applet I read the object and display the same information:

ObjectInputStream objIn = new ObjectInputStream(urlConnection.getInputStream());
Player myPlayer = (Player)objIn.readObject(); // read object from Servlet
objIn.close();

System.out.println("Applet Main getArmorClass = " + myPlayer.getArmorClass());
System.out.println("Applet Main getCharName = " + myPlayer.getCharName());

Not sure what I am doing wrong here, it seems strange that I can send a serialized object from the applet to the servlet, do my database work and build a new serialized object, but when I pass it back to the applet there is no data in it. Any suggestions would be greatly appreciated.

Thanks,
Mark
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what the problem is, but be aware that object serialization between different JVMs can easily break if the two JVMs are not the exact same version. Unless you can guarantee that, you may be better off using the java.beans.XMLEncoder/XMLDecoder classes.
 
Sheriff
Posts: 23706
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That all looks reasonable enough. But objects can't be "empty"... so what are the actual symptoms of your problem? Error messages? Stack traces? Unexpected actions?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!