HTML form POST method with querystring in action URL

10,899

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.

Share:
10,899
copenndthagen
Author by

copenndthagen

Buy some cool JavaScript related merchandise from; https://teespring.com/stores/technical-guru-2

Updated on June 15, 2022

Comments

  • copenndthagen
    copenndthagen 7 months

    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 ?

  • truthadjustr
    truthadjustr about 5 years
    You understand the question and you are able to explain it above. But my question is: which value shall prevail for a key name that is present both in the query string and in the form?
  • Darkseal
    Darkseal about 5 years
    @ifelsemonkey both of them will be returned: the first one is the one from the query string, and the second one is the one in the POST body. Also notice that getParameter() will only return the first one of them (the query string value would prevail), but you can read both of them using getParameterValues in the following way: String[] lines = request.getParameterValues("name"); for further info, read here. Edited my answer accordingly.