• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map object through servlet?

 
Vincent Oh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tim Moores
Bartender
Posts: 2957
46
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also serialize a Map as long as the Map implementation and all the keys and values are Serializable - see java.io.ObjectOutputStream.

However using object serialization requires that both client and servlet have identical classes for the serialized objects.

XML encode/decode will be a LOT easier to debug.

Bill
 
Tim Moores
Bartender
Posts: 2957
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this mean any objects can also be transferred this way?

No. The objects must adhere to JavaBean naming conventions. That's easy to implement, though.

I will have to make my object into a XML file and upload it to the servlet?

The XMLEncoder class performs the transformation to XML. It takes an OutputStream as input - e.g., the OutputStream of an URLConnection to your servlet.
 
Vincent Oh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2957
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No files are involved. The XML is created in memory, and then streamed directly to the servlet URL.

As to accessing URLs from within applets, start here: http://www.exampledepot.com/taxonomy/term/191, particularly "Getting Text from a URL" and "Sending a POST Request Using a URL".
 
Vincent Oh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2957
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

See this article on mime types for numerous examples of recognized binary formats.

Bill


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but is not the content encoded to text for transfer? (Or am I thinking of the email protocol?)
 
Vincent Oh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote: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.


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
Bartender
Posts: 2957
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, something like that. Only that you'd be reading the contents of one particular request parameter, not a file.
 
Vincent Oh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2957
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
req.getParameter("stringToReverse") is not a File, it's a String that contains the serialized XML representation.
 
Vincent Oh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2957
46
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

To a InputStream or FileInputStream to be used in


All these stream stuff is making me confused X_x
 
Tim Moores
Bartender
Posts: 2957
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Vincent Oh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... I found some weird problem, see if any of you know what's wrong. I will just paste both codes here.

This is the code to test the servlet.


This is my servlet:



variable str prints the following:



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:

java.lang.ClassNotFoundException: SimpleBean
Continuing ...
java.lang.NoSuchMethodException: <unbound>=XMLDecoder.new();
Continuing ...
java.lang.IllegalStateException: The outer element does not return value
Continuing ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic