• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • 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


 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But order is not guaranteed!
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If order is important, you can try that approach. Be careful of using the delimiter in the data values.
 
Sudipta Laha
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic