File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing custom object from jsp to applet

 
Hugh Roarty
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi can anybody help me with this jsp/servlet/applet communication problem.
I have a variable of type Customer in a jsp page and I want to send the Customer object to an applet on the same page.
I have been told the only way to do this is via a servlet.
So in the jsp page I put the Customer object named myCustomer in the session...

<%session.putValue("c", myCustomer);%>

and I am now able to get the customer object in the doGet method of the servlet...

Session s = req.getSession();
Customer jspCustomer = (Customer) s.getAttribute("c");

OK, everything works so far. I can use all the public methods of Customer within my servlet.

My problems start when I try to send my Customer object to the applet over a http connection (I have imported all the relevent libraries into the servlet)

ObjectOutputStream toApplet = new ObjectOutputStream(res.getOutputStream());
toApplet.writeObject(jspCustomer);

When I load my jsp page the applet doesn't work (HTTP error 500, i.e. the server messed up.)

The interesting thing about this problem is that if I make a Customer instance within the servlet and send this instance to the applet it works fine.
Surely my jspCustomer should behave exactly like a Customer instantiated within the servlet? If I can send one object to the applet then I should be able to send either.

Any help on this is much appreciated. Or if you think I am going in the completely wrong direction I'm interested in alternative approaches.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSPs can not reliably stream binary data to the client.
It is a templating technology designed to generate textual output like HTML, Javascript, and CSS.

Your best bet is to create a servlet that streams your objects.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps this article can help shed light on the unsuitability of JSPs to your task.
 
Hugh Roarty
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for the replies...

Yes, I am aware that JSP is unsuitable for sending binary data to the client and this is why I used a servlet as a 'go-between'. I put my object into a session on the jsp page and was then able to get that object from the session in the server. But clearly some corruption must have taken place despite using this recommended approach.

I guess I should convert my object to an XML document then send this to the applet. It'd a bit annoying since I'm using a servlet like everybody recommends and XML involves more work (I'm a bit lazy).
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17646
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you actually look at the RFCs that define the Internet, you'll discover that most of them transfer data as text. This is in large part because the Internet is composed of machines using different character sets (ASCII, EBCDIC), and different number of bits per byte/word - anywhere from 6 bits up to about 66 bits. A plus was that you could often test apps by simulating the client/server communications using telnet.

Many of the machines that the Internet had to accomdate are dead and gone, but the support lingers on.

Java has an additional problem. The serialization format for Java objects is rarely compatible between Java versions. If the server was running Java 1.4 and the applet was running under Java 1.5, I can guarantee the transfer would fail.
 
Michael Ku
Ranch Hand
Posts: 510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tipically, applet servlet communication does not involve JSPs. That is because usually use an ObjectInputstream and ObjectOutputStream to send objects across the wire. JSPs are for sending back html.

Just use the servlet to stream back your objects to the applet
 
Hugh Roarty
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks for your help guys. I'm beginning to get the picture. I think rather than send serialized objects I should send XML instead.
 
Michael Ku
Ranch Hand
Posts: 510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree. You should send serialized objects.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Ku:
I disagree. You should send serialized objects.

As Tim pointed out, this is not good advice, unless both client and server JVM are under your control.
 
Michael Ku
Ranch Hand
Posts: 510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please expand a little on this incompatibility? I have never experienced it and I currently work on a applet/servlet application.

Thank you
 
Hugh Roarty
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I've solved my problem and I'm embarrassed to say it was a bit of a silly mistake on my part. My Customer object contains an array of Product objects and although Customer implemented serializable I forgot to make Product serializable. Now that I have made Product implement serializable everything works just fine, I can send a serialized Customer object to the applet.

Thanks for all your help and sorry it turned out to be a silly mistake on my part.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic