javascript to java migeration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sai26
    New Member
    • Feb 2008
    • 10

    javascript to java migeration

    i am passing a javascript array object in the hidden field on submit, and appending
    this object to a bean class method. when i retrieve it back, the array elements
    are coming as a single string.
    i want it as a array of elements so that i can iterate through it.

    can any 1 help me out............ ...?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by sai26
    i am passing a javascript array object in the hidden field on submit, and appending
    this object to a bean class method. when i retrieve it back, the array elements
    are coming as a single string.
    i want it as a array of elements so that i can iterate through it.

    can any 1 help me out............ ...?
    I don't know JavaScript. Post a typical string as you receive it in Java and we can give better suggestions. My initial guess would be that String split method should be sufficient to parse it, but let's see a couple examples first.

    Comment

    • sai26
      New Member
      • Feb 2008
      • 10

      #3
      Originally posted by BigDaddyLH
      I don't know JavaScript. Post a typical string as you receive it in Java and we can give better suggestions. My initial guess would be that String split method should be sufficient to parse it, but let's see a couple examples first.

      http://java.sun.com/javase/6/docs/ap...a.lang.String)
      lets say i have a array instance -> aa
      frm javascript i send it as :
      document.formNa me.action = "URL&arry=" +aa;
      document.formNa me.submit();
      -------------
      in java class i set it in java bean as :
      private String[] arr;
      public void setArray(String[] arr) {
      this.arr = arr;
      }
      public String[] getArray() {
      return arr;
      }
      ------------
      so when i retrieve it, it is comming as a single string with comma saperated values. e.g a,b,c.....
      cant i get it in the array format e.g arr[0]=a,arr[1]=b ect

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by sai26
        lets say i have a array instance -> aa
        frm javascript i send it as :
        document.formNa me.action = "URL&arry=" +aa;
        document.formNa me.submit();
        -------------
        in java class i set it in java bean as :
        private String[] arr;
        public void setArray(String[] arr) {
        this.arr = arr;
        }
        public String[] getArray() {
        return arr;
        }
        ------------
        so when i retrieve it, it is comming as a single string with comma saperated values. e.g a,b,c.....
        cant i get it in the array format e.g arr[0]=a,arr[1]=b ect
        This is new to me...
        But, if you can manage the received array in that kind of format (a,b,c....)

        You can implement a function or method in java (correct me if im wrong)
        that would extract the received data format to what kind of result you are expecting....

        Then, pass it to the function that should receive the expected format...


        Guessing,
        Sukatoa

        Comment

        • sai26
          New Member
          • Feb 2008
          • 10

          #5
          Originally posted by sukatoa
          This is new to me...
          But, if you can manage the received array in that kind of format (a,b,c....)

          You can implement a function or method in java (correct me if im wrong)
          that would extract the received data format to what kind of result you are expecting....

          Then, pass it to the function that should receive the expected format...


          Guessing,
          Sukatoa
          hmm.... so u mean to use the split on the data and get it in form of array

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by sai26
            hmm.... so u mean to use the split on the data and get it in form of array
            Are you familiar with the existence of Java API documentation?



            demo:

            [CODE=Java]public class Split {
            public static void main(String[] args) {
            String data = "a,bb,ccc,dd,e" ;
            String[] tokens = data.split(",") ;
            for(String token : tokens) {
            System.out.form at("token=[%s]%n", token);
            }
            }
            }[/CODE]

            I find it a good idea when I don't understand a method to stop, and write a little code to play with it.

            Comment

            Working...