Meaningless Drivel is fun!
The moose likes JSP and the fly likes passing custom object from jsp to applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "passing custom object from jsp to applet" Watch "passing custom object from jsp to applet" New topic

passing custom object from jsp to applet

Hugh Roarty

Joined: Nov 28, 2006
Posts: 15
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());

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

Joined: Dec 11, 2004
Posts: 13410

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.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63552

Perhaps this article can help shed light on the unsuitability of JSPs to your task.

[Asking smart questions] [About Bear] [Books by Bear]
Hugh Roarty

Joined: Nov 28, 2006
Posts: 15
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

Joined: Jun 25, 2001
Posts: 17282

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.

An IDE is no substitute for an Intelligent Developer.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
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

Joined: Nov 28, 2006
Posts: 15
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

Joined: Apr 20, 2002
Posts: 510
I disagree. You should send serialized objects.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
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

Joined: Apr 20, 2002
Posts: 510
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

Joined: Nov 28, 2006
Posts: 15
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:
subject: passing custom object from jsp to applet
It's not a secret anymore!