Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML form POST method with querystring in action URL

Lets say I have a form with method=POST on my page. Now this form has some basic form elements like textbox, checkbox, etc It has action URL as http://example.com/someAction.do?param=value

I do understand that this is actually a contradictory thing to do, but my question is will it work in practice.

So my questions are;

  1. Since the form method is POST and I have a querystring as well in my URL (?param=value) Will it work correctly? i.e. will I be able to retrieve param=value on my receiving page (someAction.do)

  2. Lets say I use Java/JSP to access the values on server side. So what is the way to get the values on server side ? Is the syntax same to access value of param=value as well as for the form elements like textbox/radio button/checkbox, etc ?

like image 525
copenndthagen Avatar asked Apr 25 '12 06:04

copenndthagen


1 Answers

1) YES, you will have access to POST and GET variables since your request will contain both. So you can use $_GET["param_name"] and $_POST["param_name"] accordingly.

2) Using JSP you can use the following code for both:

<%= request.getParameter("param_name") %>

If you're using EL (JSP Expression Language), you can also get them in the following way:

${param.param_name}

EDIT: if the param_name is present in both the request QueryString and POST data, both of them will be returned as an array of values, the first one being the QueryString.

In such scenarios, getParameter("param_name) would return the first one of them (as explained here), however both of them can be read using the getParameterValues("param_name") method in the following way:

String[] values = request.getParameterValues("param_name"); 

For further info, read here.

like image 99
Darkseal Avatar answered Nov 15 '22 04:11

Darkseal