wood burning stoves 2.0*
The moose likes Servlets and the fly likes retrieving values of multiple <input> tag with same name at server side Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "retrieving values of multiple <input> tag with same name at server side" Watch "retrieving values of multiple <input> tag with same name at server side" New topic
Author

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

Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
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



ॐ सर्वे जना: सुखिनो भवन्तु , तथास्तु |
'May the whole world be happy, so be it'

SCJP1.5, SCWCD1.5
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
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.


Thanks,
Shailesh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

But order is not guaranteed!

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
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

Joined: Jan 10, 2002
Posts: 60739
    
  65

If order is important, you can try that approach. Be careful of using the delimiter in the data values.
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
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.

Regards,
Sudipta Kumar Laha
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

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.
 
subject: retrieving values of multiple <input> tag with same name at server side
 
Similar Threads
Same element name in a form to servlet
XML to Map
/test/GreetingServlet doesn't work
STRUTS: client-side validation in jsp using DynaValidatorForm
Array of Complex Type- item tag missing