Form Get Post parameter experiment

A piece of JSP code:

<h1>Form GET</h1>
<form action="/testWeb/?query-param1=value1&query-param2=value2" method="get">
<input type="text" name="form-param1" value="value1">
<input type="text" name="form-param2" value="value2">
<input type="submit" value="GET提交">
</form>
<h1>Form POST</h1>
<form action="/testWeb/?query-param1=value1&query-param2=value2" method="post">
<input type="text" name="form-param1" value="value1">
<input type="text" name="form-param2" value="value2">
<input type="submit" value="POST提交">
</form>

 Two forms, the same points :

1. The actions are the same, both are /testWeb/?query-param1=value1&query-param2=value2

2. The form fields are the same, and both contain two fields:

    <input type="text" name="form-param1" value="value1">

    <input type="text" name="form-param2" value="value2">

Two forms, different points:

Form 1: GET

Form 2: POST

 

Experimental operation:

1. Visit the JSP page: http://localhost:8080/testWeb/index.jsp

 

2. GET submit form 1. After submitting, the address bar will be changed to:

http://localhost:8080/testWeb/?form-param1=value1&form-param2=value2

 

Parameters received by the backend ( request.getParameterNames() ):

form-param1:value1 

form-param2:value2 

 

3. POST submit form 2. After submitting, the address bar will be changed to:

http://localhost:8080/testWeb/?query-param1=value1&query-param2=value2

 

Parameters received by the backend ( request.getParameterNames() ):

query-param1:value1 

query-param2:value2 

form-param1:value1 

form-param2:value2 

 

3. Modify the content of form 2 as follows (the query parameter and the form parameter both add the parameter aa, but the value is different):

<form action="/testWeb/?query-param1=value1&query-param2=value2&aa=cc" method="post">
<input type="text" name="form-param1" value="value1">
<input type="text" name="form-param2" value="value2">
<input type="text" name="aa" value="bb">
<input type="submit" value="POST提交">
</form>

 

Submit Form 2 again, with the address bar changed to:

http://localhost:8080/testWeb/?query-param1=value1&query-param2=value2&aa=cc

 

Parameters received by the backend:

query-param1:value1 

query-param2:value2 

aa:cc, bb 

form-param1:value1 

form-param2:value2 

 

 

Summarize:

1. When GET submits the form, the query parameter in the action address will be discarded, and the backend cannot get it (even if it is an empty form, that is, a form without any input fields).

2. When POST submits the form, the query parameter in the action address will not be discarded, and will be submitted to the backend together with the form parameter.

3. When POST submits the form, if the query parameter in the action address has the same name as the form parameter, it will also be submitted to the backend together, and the array obtained by the backend request.getParameterValues(parameterName) will contain multiple values.

4. The parameters obtained by request.getParameter() are not divided into query parameters and form parameters, nor are they divided into get parameters and post parameters.

5. If you only want to get the query parameter, you can get the query string through request.getQueryString(), and then separate it yourself.

6. When the backend obtains parameters through request.getParameterNames (), the query parameter in the address bar is in the front, and the POST form parameter is in the back.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326305846&siteId=291194637