drfloob
drfloob

Reputation: 3264

How to POST multiple "record sets" from an HTML Form to JSP or a Java Servlet?

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:


Related Links:

Upvotes: 2

Views: 5399

Answers (1)

Adeel Ansari
Adeel Ansari

Reputation: 39907

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);
}

Upvotes: 5

Related Questions