13

I know that I can get a parameter like:

HTML

<input type="text" name="field" value="test">

Servlet

String field = request.getParameter("field");

But what if I have multiple input with same name like:

HTML

<input type="text" name="line[]" value="test1">
<input type="text" name="line[]" value="test2">
<input type="text" name="line[]" value="test3">

In PHP I can just use name="line[]" to get an array of all the line inputs. But how to go about this in java?

Servlet pseudo code

String[] lines = request.getParameterArray("line");

for(String line : lines){
    //do shit
}
botenvouwer
  • 4,334
  • 9
  • 46
  • 75
  • http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getParameterValues%28java.lang.String%29 – Sotirios Delimanolis Jun 15 '15 at 15:58
  • Here's some background information why weak typed languages like PHP use the awkward `[]` suffix convention: http://stackoverflow.com/a/3061292 – BalusC Jun 15 '15 at 16:34

1 Answers1

17

Close. It's

String[] lines = request.getParameterValues("line");

but the name is line, not line[]

NickJ
  • 9,380
  • 9
  • 51
  • 74