Let’s assume I have a form with these fields (HTML)
<input type="text" name="user[name]" value="name"/>
<input type="text" name="user[email]" value="email"/>
<input type="text" name="user[age]" value="age"/>
how do I get this information inside a java Servlet?
In php it is just
$_POST["user"]["name"]
or
$_GET["user"]["name"]
I am not referring at simple field like
<input type="text" name="user" value="jimmy"/>
I am referring at retrieving arrays of data, like in the case of a set of checkboxes or a multiple select.
One thing to keep in mind, is that the brackets in the field names have no special meaning and are simply part of the name, so you’d have to include them when using
ServletRequest::getParameter:It’s only PHP (or another additional framework), that “automagically” treats names with brackets as an array (or in case of Java more likely a
Map).If you what such a map and you are not using a such framework you’d need to create it yourself, for example, by looping over
ServletRequest::getParameterNames.Example:
This is just a quick (untested) example. It doesn’t support multiple level maps (
user[name][last]) nor multiple values for one name (which needgetParameterValues).