Question

Does JSP or any related lightweight technology like JSTL perform HTTP POST "data grouping", or support form element "indexing" in the way PHP does?

For example, you can create an HTML form with the following inputs:

<input type="text" name="person[1][name]" />
<input type="text" name="person[1][age]" />
<input type="text" name="person[2][name]" />
<input type="text" name="person[2][age]" />

... and PHP will parse that into a nested associative array automatically. Do JSP, Java Servlets, or any related spec or tool provide this kind of translation out of the box?

The goal is to submit multiple "record groups" in a single form, and process them server-side in JSP or a Servlet.


Requirements:

  • The functionality cannot rely on JavaScript
  • No full frameworks like Spring, Struts, or the like
  • I'm trying to avoid reinventing the wheel with my own naming convention and manual String parsing / Regex

Related Links:

Was it helpful?

Solution

Try this,

<input type="text" name="personNames" />
<input type="text" name="personAges" />
<input type="text" name="personNames" />
<input type="text" name="personAges" />

You should consider to create input fields using a loop, you don't need to postfix the name even. and get parameter values like this in your servlet,

String[] names = request.getParameterValues("personNames");
String[] ages = request.getParameterValues("personAges");

It will come in the same order as defined in your HTML. Then loop over it like below,

for( String name : names) {
   System.out.println(name);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top