This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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...
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)
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.
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).
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.
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.
Joined: Apr 20, 2002
Could you please expand a little on this incompatibility? I have never experienced it and I currently work on a applet/servlet application.
Joined: Nov 28, 2006
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.