Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Servlet - get parameters with same name

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
}
like image 619
botenvouwer Avatar asked Jun 15 '15 15:06

botenvouwer


People also ask

Which request parameters are stored for Java servlets?

For HTTP servlets, parameters are contained in the query string or posted form data. If the parameter data was sent in the request body, then i occurs with an HTTP POST request. Data from the query string and the post body are aggregated into the request parameter set.

Do method GET parameters?

The doGet( ) method displays the form itself. The doPost( ) method handles the submitted form data, since in doGet( ), the HTML form tag specifies the servlet's own address as the target for the form data. The servlet (named FirstServlet) specifies that the declared class is part of the com.

What is RequestDispatcher and its methods?

The RequestDispatcher is an Interface that comes under package javax. servlet. Using this interface we get an object in servlet after receiving the request. Using the RequestDispatcher object we send a request to other resources which include (servlet, HTML file, or JSP file).


Video Answer


1 Answers

Close. It's

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

but the name is line, not line[]

like image 82
NickJ Avatar answered Sep 21 '22 08:09

NickJ