best way to post data from a javascript to a servlet

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Fish

    #1

    best way to post data from a javascript to a servlet

    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


  • Douglas Crockford

    #2
    Re: best way to post data from a javascript to a servlet

    > I have a webpage with some javascript that constructs some data structures,[color=blue]
    > 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[/color]

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



    Comment

    • Andy Fish

      #3
      Re: best way to post data from a javascript to a servlet

      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


      "Douglas Crockford" <nospam@laserli nk.net> wrote in message
      news:bhil6i$nc6 $1@sun-news.laserlink. net...[color=blue][color=green]
      > > I have a webpage with some javascript that constructs some data[/color][/color]
      structures,[color=blue][color=green]
      > > some of which contain nested structures or arrays of other structures.[/color][/color]
      It's[color=blue][color=green]
      > > 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[/color]
      >
      > I would do (b), using JSON to format the data.
      >
      > http://www.JSON.org/
      >[/color]


      Comment

      • Andy Fish

        #4
        Re: best way to post data from a javascript to a servlet

        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


        "Andy Fish" <ajfish@blueyon der.co.uk> wrote in message
        news:DE5%a.2402 $vK5.16153719@n ews-text.cableinet. net...[color=blue]
        > 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[/color]
        some[color=blue]
        > 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[/color]
        in[color=blue]
        > very handy ;-)
        >
        > I'll let you know how I get on.
        >
        > Andy
        >
        >
        > "Douglas Crockford" <nospam@laserli nk.net> wrote in message
        > news:bhil6i$nc6 $1@sun-news.laserlink. net...[color=green][color=darkred]
        > > > I have a webpage with some javascript that constructs some data[/color][/color]
        > structures,[color=green][color=darkred]
        > > > some of which contain nested structures or arrays of other structures.[/color][/color]
        > It's[color=green][color=darkred]
        > > > 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[/color][/color][/color]
        the[color=blue][color=green][color=darkred]
        > > > best way to do it. It has to be browser-independent. The only ways I[/color][/color][/color]
        can[color=blue][color=green][color=darkred]
        > > > 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?)[/color][/color][/color]
        and[color=blue][color=green][color=darkred]
        > > > then post that[/color]
        > >
        > > I would do (b), using JSON to format the data.
        > >
        > > http://www.JSON.org/
        > >[/color]
        >
        >[/color]


        Comment

        Working...