views:

28

answers:

2

I have a number of textboxes that will be displayed with their existing values. I want my servlet to be able to get all their values and then update the database with the the values that have changed.

How do I get the values? Is there some way to put them into a HashMap with the id or name as the key and the value of the textbox as the value for the key?

+2  A: 

If you're giving all of the text boxes the same name, you can get a String[] of the values via ServletRequest.getParameterValues.

If you want to get all of your submitted fields in a single map, you can use ServletRequest.getParameterMap to get a Map of all of the parameters submitted. Each individual parameter's value in the map is a String[].

Here's some sample code that walks through all submitted parameters and all of their values:

Iterator    it;
Map         params;
String      name;
String[]    values;
int         n;

params = request.getParameterMap();
it = params.keySet().iterator();
while (it.hasNext())
{
    name = (String)it.next();
    values = (String[])params.get(name);
    for (n = 0; n < values.length; ++n)
    {
        // ...use value[n]...
    }
}
T.J. Crowder
Thanks ... I probably should have googled a little more first.
Ankur
thanks for the code
Ankur
@Ankur: No worries, hope that helps.
T.J. Crowder
+1  A: 

Or you can use HttpServletRequest.getParameterValues("someName") where all text inputs have name="someName"

Bozho
LOL Jinx! Just got back from adding that to my answer (since he'd said it was a bunch of text boxes) and saw your post.
T.J. Crowder