开发者

I have a variable number of textboxes how do I retrieve their values in a servlet

I have a number of textboxes that will be disp开发者_高级运维layed 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?


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]...
    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜