Sending HTML form data array to JSP/Servlet - html

I am coming from the PHP world, where any form data that has a name ending in square brackets automatically gets interpreted as an array. So for example:
<input type="text" name="car[0]" />
<input type="text" name="car[1]" />
<input type="text" name="car[3]" />
would be caught on the PHP side as an array of name "car" with 3 strings inside.
Now, is there any way to duplicate that behavior when submitting to a JSP/Servlet backend at all? Any libraries that can do it for you?
EDIT:
To expand this problem a little further:
In PHP,
<input type="text" name="car[0][name]" />
<input type="text" name="car[0][make]" />
<input type="text" name="car[1][name]" />
would get me a nested array. How can I reproduce this in JSP?

The [] notation in the request parameter name is a necessary hack in order to get PHP to recognize the request parameter as an array. This is unnecessary in other web languages like JSP/Servlet. Get rid of those brackets
<input type="text" name="car" />
<input type="text" name="car" />
<input type="text" name="car" />
This way they will be available by HttpServletRequest#getParameterValues().
String[] cars = request.getParameterValues("car");
// ...
See also:
Send an Array with an HTTP Get

Related

Html form get request not adding the form data

I'm making a desktop app that requires user to activate a key.
So the form here is making a get request to the api.
How do I make it so that when the user submits a form data (a key)
it is added as a parameter in the string?
UPDATE: Question answered.Thanks!
But can anyone tell me how to make it so if the key is valid, the software loads, but if not then it throws an exception?
<form method="get" formenctype="text/plain" action="https://app.cryptolens.io/api/key/Activate?token=blahblahblah&ProductId=xxx2&Key=" >
<input type="text" maxlength="23" size="80" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
</form>
I don't think you quite understand how GET works. Get sends data via the URI, any field(using the 'name' property) you add into your form is added to the query string.
<form method="get" formenctype="text/plain" action="https://app.cryptolens.io/api/key/Activate" >
<input type="text" name="token" maxlength="23" size="80" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
<input type="text" name="ProductId" maxlength="23" size="80" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
<input type="text" name="otherData" maxlength="23" size="80" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
</form>
This will produce: https://app.cryptolens.io/api/key/Activate?token=VALUE&ProductId=VALUE&otherData=VALUE
Note I also removed the query string from the ACTION="" As the request will do that automatically for you.
EDIT: I also wouldn't recommend having your token in plain view. Either you make it a hidden field or you use javascript to execute the request with some masking. Hidden is usually fine providing the API has the necessary security to handle it.
Using hidden inputs and naming them like the GET you need.
Check the name token with hidden value like example.
Remove all the GET from the action-url.
The final URL will be: https://app.cryptolens.io/api/key/Activate?Key=yourKey&token=blahblahblah&ProductId=xxx2
<form method="get" formenctype="text/plain" action="https://app.cryptolens.io/api/key/Activate" >
<input type="text" maxlength="23" size="80" name="Key" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
<input type="hidden" name="token" value="blahblahblah" />
<input type="hidden" name="ProductId" value="xxx2" />
</form>

HTML code to send SMS message through a form

I am trying to create an HTML code in order to send SMS messages through a form.
My provider give to me the Http address that i could send these SMS and it looks like bello (of course i have hide passwords, etc)
http://sms.services/send-sms?app-id=108744&password=1111111&to={1}&from=TestSender&message={0}
When i call this address with a phone number instead of {1} and a text insted of {0} it works fine.
But when i try to create the bellow page it says that the attributes are wrong.
Can you please help?
<form action="http://sms.services/send-sms?app-id=108744&password=1111111&to=telnumber&from=TestSender&message=textsms" method="post">
<input type="number" name="telnumber" />
<input type="text" name="textsms" />
<input type="submit" value="Submit" />
Change your method to GET
Make other parameters input type hidden so user will not see those but internally it will be passed.
<form action="http://sms.services/send-sms" method="get">
<input type="number" name="to" />
<input type="text" name="message" />
<input type="hidden" name="app-id" value="108744" />
<input type="hidden" name="password" value="1111111" />
<input type="hidden" name="from" value="TestSender" />
<input type="submit" value="Submit" />
</form>
Note: It is not recommended to do like it because it will expose your app-id and password. You should send it to a php or any backend framework and then from there call this.
Try to use input type="text" instead of number.
Probably, input type="number" tries to convert string to demical, but your provider use string

HTML Form POST re-writing action attribute

I have an HTML form that I'm trying to get to post with part of a query string already inplace, but it keeps re-writing the URL.
<form id="mls_form" action="/index.php?option=com_mls&view=mls" method="get">
<label>MLS#:</label>
<input type="text" name="mlsnum" />
<input type="submit" value="Go" />
</form>
Output is:
http://www.mysite.com/index.php?mlsnum=value
It seems really simple, but I don't know why it's re-writing the action attribute.
Use the POST method rather than the GET method. The URL parameters will be sent as specified in the action attribute, and the form inputs will be sent in the post data. Your server script can then read them each using whatever API is appropriate (in PHP, $_GET versus $_POST, or find them all in $_REQUEST).
If you must use GET you can give the additional parameters as hidden input fields.
<form id="mls_form" action="/index.php" method="GET">
<input type="hidden" name="option" value="com_mls" />
<input type="hidden" name="view" value="mls" />
<label>MLS#:</label>
<input type="text" name="mlsnum" />
<input type="submit" value="Go" />
</form>

How to handle dynamic number of form elements in javaee6

Normally we have a static form that is posted on the server and since it's static we know the name of the variables.
But let's say I have a dynamic number of form elements that I need to post, what's the best way to handle it in the backend? Can it be done via jersey?
A best example of this, is paypal's implementation when checking out items. They have dynamic number of inputs that is suffixed with _x, where x is a number.
<form>
<input type="text" name="name_1" />
<input type="text" name="amount_1" />
<input type="text" name="quantity_1" />
<input type="text" name="name_2" />
<input type="text" name="amount_2" />
<input type="text" name="quantity_2" />
</form>
How do I read all the input text in the backend?
Thanks,
czetsuya
you can read entity as MultivaluedMap<String, String> form or Form form (which is basically same thing) and iterate all keys or .. whatever you need. See FormParamTest for example.

POST extra values in an HTML <form>

I have a simple form which passes the value of an <input /> element:
<form action="updaterow.php" method="POST">
<input type="text" name="price" />
</form>
How can I post extra values along with the <input /> value? For example, an arbitrary string, or a variable inside the current PHP script.
I know this is possible with GET:
<form action="updaterow.php?foo=bar" method="GET">
<input type="text" name="price" />
</form>
or:
<form action="updaterow.php?foo=<?=htmlspecialchars($bar)?>" method="GET">
<input type="text" name="price" />
</form>
but I need POST.
You can include a hidden form element.
<input type="hidden" name="foo" value="bar" />
You can simply use a hidden field. Like so:
<input type="hidden" name="your-field-name" value="your-field-value" />
This field will then be available in your PHP script as $_POST['your-field-name']
As mentioned already, using hidden input fields is an option, but you can also use a session. That way you donĀ“t have to post anything, the variables remain on the server and are not exposed in the html.