Passing Javascript array to servlet

Discussion in 'Javascript' started by Garg, Feb 8, 2007.

  1. Garg

    Garg Guest

    Hi All,

    I am facing one problem if you are having any solution please tell me.

    I have to pass an array from javascript to servlet. for this i created
    one array and pass that through submitting the form with post method
    and i am using request.getParameterValues to get that array. But i am
    getting values in the first position of that array and that also comma
    separated so that also of no use for me.

    Is there any way to pass the value to the Servlet?

    Thanks
    Tarun Garg
     
    Garg, Feb 8, 2007
    #1
    1. Advertising

  2. Garg

    Erwin Moller Guest

    Garg wrote:

    > Hi All,
    >
    > I am facing one problem if you are having any solution please tell me.
    >
    > I have to pass an array from javascript to servlet. for this i created
    > one array and pass that through submitting the form with post method
    > and i am using request.getParameterValues to get that array. But i am
    > getting values in the first position of that array and that also comma
    > separated so that also of no use for me.
    >
    > Is there any way to pass the value to the Servlet?
    >
    > Thanks
    > Tarun Garg


    Hi Garg,

    You can do many things, but I think the easiest is simple define your own
    datastructure, or better said: data encodingscheme.

    A simple example:
    Suppose you have (in JS) an array that contains only numbers, and want to
    pass that.
    convert:
    Array(1,4,67,897,1254);
    to a string:
    "1,4,67,897,1254"
    and put that string into a hidden field (or a normal textfield, or whatever
    part of your form that can hold a string).

    When the servlet receives the Form, you can simple get the string, split it
    by ',' and create a new array in Java that you populate with the seperate
    values.

    If you have to send more complex information, eg if you JS array contains
    characters and number, you'll have to encode the characters before adding
    them to a string.
    eg:
    If you have this:
    Array(1, 'bla,di,bla', 45)
    You'll have top make sure the , in 'bla,di,bla' are encoded so you can split
    the string when it arrives.

    I never had a real-life situation where I couldn't encode an Array in the
    simple way I just described, BUT: If your array is really complex, and
    contains arrays itself, you better start looking at other solutions, maybe
    JSON (I never used that, so maybe I talk nonsense).


    Good luck,

    Regards,
    Erwin Moller
     
    Erwin Moller, Feb 8, 2007
    #2
    1. Advertising

  3. Erwin Moller schreef:
    > I never had a real-life situation where I couldn't encode an Array in the
    > simple way I just described, BUT: If your array is really complex, and
    > contains arrays itself, you better start looking at other solutions, maybe
    > JSON (I never used that, so maybe I talk nonsense).


    JSON would work perfectly. It has libraries in almost all modern
    languages allowing you to transparently change a Javascript array into
    an array in any other programming language.
     
    Frederik Vanderstraeten, Feb 8, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    2,432
    Andy Fish
    Dec 17, 2003
  2. Sean Clarke
    Replies:
    1
    Views:
    1,922
    Sudsy
    Jan 7, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    2,045
    Jack Jia
    Apr 4, 2004
  4. Andrew Purser
    Replies:
    3
    Views:
    5,137
    Alex Kay
    Nov 4, 2004
  5. Garg
    Replies:
    2
    Views:
    1,757
Loading...

Share This Page