Reputation: 51110
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?
Upvotes: 1
Views: 825
Reputation: 1074545
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]...
}
}
Upvotes: 2
Reputation: 597124
Or you can use HttpServletRequest.getParameterValues("someName")
where all text inputs have name="someName"
Upvotes: 2