wood burning stoves 2.0*
The moose likes Servlets and the fly likes dynamically loop through form parameters in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "dynamically loop through form parameters in servlet" Watch "dynamically loop through form parameters in servlet" New topic
Author

dynamically loop through form parameters in servlet

John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
I have a html table that has 4 columns. This table is built from the database and can have any number of rows. For the first column of every row there is a check box. What I need to do is when this form gets posted to my servlet i need to loop though and get the parameters for each row of the table and update the corresponding row in my database. Below is the form i am using. I am not sure the best way to do this in the servlet and i am looking for some direction. Also does each of the rows components have to have a different name?



Servlet attempt
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

If you name all the fields the same, you can get an array of the values with request.getParameterValues(), but how would tell which belongs to which row? Order is not guaranteed.

Is the vCode value unique to each row? If so, you could incorporate it into the names used on that row.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
yes vCode is unique. Should i also put id's on the form fields and have them with unique values or do i not even need the id's?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

id's are for client-side code. If you don't need to refer to the elements in JavaScript code, id's are not needed.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
With the help of Bear

thank you Bear.

If any one is having the same problem this was my resolution:
Should anyone see that it can be improved in any way please post a response
all my fields were named the same with the vendorcode appended on the end to diferentiate the rows.

Form:


Servlet:


[ October 26, 2008: Message edited by: John Schretz ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Thanks for posting back your solution! It's sure to help future posters running into similar needs.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Anytime HTH
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: dynamically loop through form parameters in servlet
 
Similar Threads
Rounding off $$ amounts
tiles and <sx:autocompleter>
Error comes whilke compiling the jsp page
How to get a scriptlet value for javascript validation
Tiles error, not able to find a value for attribute