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
}
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.
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.
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).
Close. It's
String[] lines = request.getParameterValues("line");
but the name is line
, not line[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With