Is it possible to send a Java Map object to the servlet? I'm doing an applet that will create a Map object which is required to be saved into a file at the server side.
If it is not possible, any ways to go about doing it?
Sure. You can use the java.beans.XMLEncoder/XMLDecoder classes to serialize Java objects that follow the JavaBean conventions into XML, which you can then send to the server via HTTP.
Vincent Oh
Ranch Hand
Joined: Jan 02, 2012
Posts: 33
posted
0
Does this mean any objects can also be transferred this way?
So by using this method, I will have to make my object into a XML file and upload it to the servlet?
I'm still abit confused. Do I pass the XML files to the servlet like how I do a file upload, then read the xml file data?
Eventually, I will need my applet do send the info to the servlet, which will then update my database. Maybe someone will be so kind to guide me on how to do this? Thanks alot!
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
0
No files are involved. The XML is created in memory, and then streamed directly to the servlet URL.
Tim Moores wrote:No files are involved. The XML is created in memory, and then streamed directly to the servlet URL.
How do you actually type the code to stream the XML over?
This is the encode code I got from one of the example site, not sure how to use it with the above.
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
0
Something like this:
Then the XML can be retrieved as request parameter "stringToReverse" (and decoded by the XMLDecoderclass). The details may be wrong, but that's the general idea. Generally, you shouldn't call String.getBytes() without specifying an encoding, but since it's all ASCII (in line 6), it's OK in this case.
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12327
1
posted
0
Basically, just be aware that HTTP is a text protocol. Anything passed to and fro via HTTP must be text.
Oh Bear! What a clunker!
Anything passed in the headers must be text but the body can be anything. How else do we transmit images, pdfs, sounds, zip files and and serialized objects.
Then the XML can be retrieved as request parameter "stringToReverse" (and decoded by the XMLDecoderclass). The details may be wrong, but that's the general idea. Generally, you shouldn't call String.getBytes() without specifying an encoding, but since it's all ASCII (in line 6), it's OK in this case.
So meaning at my servlet, I will just have to do something like:
Then I will get my Bean object back, thus, allowing me to do whatever server update?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
0
Yes, something like that. Only that you'd be reading the contents of one particular request parameter, not a file.
Vincent Oh
Ranch Hand
Joined: Jan 02, 2012
Posts: 33
posted
0
Not sure if I did anything wrong, kindly help me check, thanks!
I'm trying to let the servlet return some message so I can print it out and check. What I'm doing now is using a normal J2SE class to send a SimpleBean over, decode it at the server, and return the name of the bean to the J2SE class to print. Not sure why it is giving me Server returned HTTP response code: 500 for URL: http://127.0.0.1:8080/DESPort/desport/reverse.
PS. the link for the servlet "Reverse" is correct.
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
0
req.getParameter("stringToReverse") is not a File, it's a String that contains the serialized XML representation.
Vincent Oh
Ranch Hand
Joined: Jan 02, 2012
Posts: 33
posted
0
Hmmm... I know how to read a normal XML file, but to read a String representation of a XML in memory, is there any proper way to do it, or I just have to tokenize the String or "search" through the String?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2407
posted
1
That's what the XMLDecoder class does - it returns the deserialized object. You need to create an InputStream that reads the bytes of the request parameter; the java.io package has a class that's very helpful for that.
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12327
1
posted
0
Bear Bibeault wrote:Yes, but is not the content encoded to text for transfer? (Or am I thinking of the email protocol?)
Yep, you were thinking of email - a hangover from ancient days of teletypes, 7bit ASCII and a lot of stuff I have forgotten the details of.
A hazard of being in the biz a long long time....
Bill
Vincent Oh
Ranch Hand
Joined: Jan 02, 2012
Posts: 33
posted
0
Hmm... I only manage to read my XML content after I create a file, then re-read the file to decode it to a Bean.
Can't seems to find the correct way to convert from
OK, one more hint: The way to get a byte[] from a String is the String's getBytes() method. And I already mentioned the ByteArrayInputStream class.
Vincent Oh
Ranch Hand
Joined: Jan 02, 2012
Posts: 33
posted
0
Tim Moores wrote:OK, one more hint: The way to get a byte[] from a String is the String's getBytes() method. And I already mentioned the ByteArrayInputStream class.
Do you mean something like this?
Somehow it gave me a error message on my tomcat's log:
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
Geez, I'm running out of time already X_x stuck on this for a few days...
It's preferable to parse from a StringReader, rather than converting the String to bytes using an unknown encoding and then passing those bytes to the parser to be converted back to chars using a potentially different encoding.
As for your most recent exception, you do need to pass a well-formed XML document to the parser. Looks to me like you passed an empty string.
Vincent Oh
Ranch Hand
Joined: Jan 02, 2012
Posts: 33
posted
0
Paul Clapham wrote:It's preferable to parse from a StringReader, rather than converting the String to bytes using an unknown encoding and then passing those bytes to the parser to be converted back to chars using a potentially different encoding.
As for your most recent exception, you do need to pass a well-formed XML document to the parser. Looks to me like you passed an empty string.
The variable str prints the following
So I'm not really sure how to parse this String into a XMLDecoder to get my SimpleBean object back. Someone please kindly give me the code to parse the String into an InputStream or something if my code from the previous post is wrong.
After some testing, it seems that Class is not found at the server side for SimpleBean, which is indeed existing though. Any ideas anyone? The error logged from tomcat is as followed: