best way to post data from a javascript to a servlet

A

Andy Fish

Hi,

I have a webpage with some javascript that constructs some data structures,
some of which contain nested structures or arrays of other structures. It's
the same order of complexity as a web.xml file, say.

I need to post the whole lot to the server and I'm not sure which is the
best way to do it. It has to be browser-independent. The only ways I can
think of are:

(a) construct a form on the fly with lots of controls, one for each
primitive value
(b) put the whole lot in a big text box (but in which format - xml?) and
then post that

Any advice or ideas would be most welcome.

Andy
 
D

Douglas Crockford

I have a webpage with some javascript that constructs some data structures,
some of which contain nested structures or arrays of other structures. It's
the same order of complexity as a web.xml file, say.

I need to post the whole lot to the server and I'm not sure which is the
best way to do it. It has to be browser-independent. The only ways I can
think of are:

(a) construct a form on the fly with lots of controls, one for each
primitive value
(b) put the whole lot in a big text box (but in which format - xml?) and
then post that

I would do (b), using JSON to format the data.

http://www.JSON.org/
 
A

Andy Fish

I'm guessing you are not entirely a disinterested party where JSON is
concerned :)

nevertheless, I'm very tempted to give it a go, especially as you have some
java classes on hand to parse the resulting output. Now, if you could just
add an XSLT that generates JSON Javascript declarations, that would come in
very handy ;-)

I'll let you know how I get on.

Andy
 
A

Andy Fish

I'd just like to report that I downloaded JSON from douglas' site and used
it to serialize my javascript objects and parse them again in java in the
servlet.

Everything worked perfectly first time except there was no definition of the
variable 'isNetscape4' (presumably I was intended to figure this out
myself). I would recommend this solution for anyone who wants to take some
data structures they have created in javascript and post them into a
servlet, or for any other of the applications mentioned on
www.crockford.com.

However, I have to say that I am not 100% convinced by the arguments on the
web site about not using XML. I think it would have been possible to use a
subset of XML (no namespaces, schemas, encodings, only the built-in
entities) and this would give the receiver of the object the option of using
full-blown XML tools to process it.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top