• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

retrieving values of multiple <input> tag with same name at server side

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

my code dynamically add two input fields for key and value which I want to retrieve at server side in a servlet code.[in following code I have shown two only ]


Can anyone tell me how should I retrieve these key and value at server side please?

If I use request.getParameterValues("key") [assume first key has value 'Name' and second has 'Surname'], at server side I recieve String array, is it guaranteed that they will be in order i.e array[0] = Name and array[1] = Surname ?

Thanks
Bharat


 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Bharat,

If you enter Name in first kay feild & Surname in second Kay field then you will get array[0] = Name and array[1] = Surname.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But order is not guaranteed!
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies guys ! But can anybody tell me how should I handle such requirement ?

I am thinking of appending all the key value [comma separated] to a hidden field and same for the value field is this good approach ?

Thanks
Bharat
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If order is important, you can try that approach. Be careful of using the delimiter in the data values.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharat,

If the HTML is dinamically generated then we can set the input fields name as key0,key1,key2...
Which will help in the servlet to get the parameters.

Another option may be:
If its a static html we can assign the input type names as key0,key1,key2 in javascript.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using index values (0,1,2) is another alternative, but it's rather clunky. You might also use JSON in a hidden input, or even use suffixed such as [0], [1] and so on and use indexed bean properties.

Personally, because google's Gson library makes JSON so convenient, I use it often for such purposes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic