tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Warnier (tomcat) ...@ice-sa.com>
Subject Re: Access hidden fields array
Date Wed, 03 May 2017 10:05:48 GMT
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.


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


Mime
View raw message