Hi again.

First, on this list please do not top-post.
See : http://tomcat.apache.org/lists.html#tomcat-users
  Important --> 6

I have reformated your message according to that.
>
2017-05-03 12:05 GMT+02:00 André Warnier (tomcat) <a...@ice-sa.com>:

On 03.05.2017 11:23, Tobias Soloschenko wrote:

Hello everyone,

I just updated to tomcat 8.0.43 and I noticed a special behavior I want to
discuss. (I don't know if this only occurs with this version)

When I add two hidden fields within a form like:

<input type="hidden" name="array[]" value="1" />

<input type="hidden" name="array[]" value="2" />


I just thought to access the parameter values within the java api like

String[] myArray = httpServletRequest.getParameterValues("array");


In my case however I just can access them like (with brackets):

String[] myArray = httpServletRequest.getParameterValues("array[]");


This also applies to httpServletRequest.getParameterMap().containsKey


Is this an expected behavior?



I would tend to say : yes, it is expected.
(This also has nothing to do with tomcat per se, and is more of the domain
of the HTML and HTTP specifications).

I have not checked the relevant HTML/HTTP specs to verify which characters
precisely are allowed in the "name" attribute of a HTML <form> field.
But assuming that "[" and "]" /are/ allowed in such attributes, then that
is the name under which the value of this parameter will be sent by the
browser to the server.

More precisely : if you have in your form an <input> element such as :

<input type="hidden" name="xyz[123abc]" value="1" />

(whatever "xyz[123abc]" may be, in terms of valid characters)
then "xyz[123abc]" is the name/label of this parameter, and the browser
will send the corresponding input field value to the server as something
like :

xyz[123abc]=1

The fact that this label /resembles/ the way in which you would invoke an
array in Java (or any other server-side programming language) is pure
coincidence.

In other words again, in your above example, if you replaced "array[]" by
"array][" (or "array)(", or "array(xyz)" or "variable#1") everywhere, it
would work just the same. It's just a label.

And the fact that in your example they are seen as an array on the java
side, is just because there is more than one value sent by the browser with
that same label, and the POST parsing logic on the server side then makes
this into an array of values.

This all being said, I would not name any input fields in a form with a
name like "array[]". That is bound to create confusion, as you yourself can
now attest.
You can name is "ferrari" instead; but that does not mean that on the
server side, you should expect something red with 4 wheels and 12 cylinders.



On 03.05.2017 12:48, Tobias Soloschenko wrote:
Hi,

thanks for the detailed answer.

The reason why I used the brackets was that in some cases (browsers) only
the first value was returned. (Even if there were more then one hidden
field with that name)

Also mentioned here: http://stackoverflow.com/a/6547234 - PHP is also
resolving the variable without brackets.

There are various other questions at stackoverflow mentioning to get the
variable without brackets:

http://stackoverflow.com/questions/6547209/passing-array-using-html-form-hidden-element
http://stackoverflow.com/questions/4237090/how-to-pass-array-through-hidden-field
http://stackoverflow.com/questions/29076219/javascript-storing-array-of-objects-in-hidden-field

Don't know if this should be supported.


There is something confusing in your posts so far.
This is a Tomcat user's list. Tomcat is a Java Servlet Engine, written in Java.
Your example above also seemed to be Java.
But the links you propose above all relate to PHP applications, and they are a bit confusing (and sometimes also confused) themselves. I mean, there is some information in them that may be relevant to your issue, but also a lot of noise that is irrelevant here.

There are 3 parts to your issue :
1) how the HTML form is generated, before it is sent to the browser
2) once it is "in" the browser, what the <form> looks like (in terms of input fields and values), and how its content will be sent to the server when the user presses the submit button 3) the application, on the server side, which will parse this POST content, and how the data will look to it

Only (3) is really relevant on this list.

And as far as that is concerned :

A) if your <form> contains 3 input elements like
 <input type="hidden" name="f1" value="1" />
 <input type="hidden" name="f1" value="2" />
 <input type="hidden" name="f1" value="3" />

then what the browser sends to the server (in one way or another), will look 
like

f1=1
f1=2
f1=3

and, in a Java application on the server, to get these values you would use 
something like :

String[] myArray = httpServletRequest.getParameterValues("f1");

and in myArray, you would get [ "1", "2", "3" ]

and this is true whatever the name "f1" would be replaced by, in the <form> as well as in the Java code.

B) if your <form> contains 3 input elements like
<input type="hidden" name="f1" value="1" />
<input type="hidden" name="f2" value="2" />
<input type="hidden" name="f3" value="3" />

then what the browser sends to the server (in one way or another), will look 
like

f1=1
f2=2
f3=3

then, in the Java application on the server, if you use something like :

String[] myArray = httpServletRequest.getParameterValues("f1");

in myArray, you would get [ "1" ]

and to get the other values, you would have to do
String[] myArray2 = httpServletRequest.getParameterValues("f2");  // myArray2 = [ 
"2" ]
String[] myArray3 = httpServletRequest.getParameterValues("f3");  // myArray3 = [ 
"3" ]

C)  if your <form> contains 1 input element like
<input type="hidden" name="f1" value="1;2;3" />

then what the browser sends to the server (in one way or another), will look 
like

f1=1;2;3

then, in the Java application on the server, if you use something like :

String[] myArray = httpServletRequest.getParameterValues("f1");

in myArray, you would get [ "1;2;3" ]

and if you want to parse this back into an array of 3 elements [ "1", "2", "3" ], you would have to do that yourself.

And again, this does not vary, whatever "name" you are using for that/these 
parameter(s),
and if the browser makes any difference, then the browser has a bug.

Said another way again : there is no "magic" way (such as some special parameter "name") that allows a browser to send data to a server as an array. That is simply not part of the HTTP protocol. All the <form> values are sent to the server as text. If you want to implement some special encoding method in such text, that allows you to send to the browser a single text field value which in reality is composed of multiple separate values, and vice-versa to allow your application to parse such a text back into an array of values, you have to implement your own scheme for doing that. Some of the links which you mention in your previous message provide suggestions (right or wrong) of how one could do this in PHP, but that is not really relevant here.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to