Accessing HTML-Input field arrays in Java Servlet

2019-08-13 11:06发布

问题:

I've got a dynamic number of fieldsets with 3 input fields each ordered differently, for example:

<fieldset>
  <input type="text" name="fieldset[1][valueA]">
  <input type="text" name="fieldset[1][valueB]">
  <input type="text" name="fieldset[1][valueC]">
</fieldset>
<fieldset>
  <input type="text" name="fieldset[2][valueC]">
  <input type="text" name="fieldset[2][valueB]">
  <input type="text" name="fieldset[2][valueA]">
</fieldset>

I would like to access these fields within a Java servlet in a loop, but I don't know how to adress these fields.

It would be possible with the input's name this way

fieldset[n][]

and a loop this way (to access the fields of fieldset[1]

String[] elements;
elements = request.getParameterValues("fieldset[1]");
for(int i = 0; i < elements.length; i++) {
out.write(elements[i]);
}

But is there any way I can do this and at the same time keep the information in the second bracket?

回答1:

It seems The using [] notation causing the problem .

The HttpServletRequest.getParameterValues() can loop like in the below link.

check this answer