Form Get Post 参数 实验

一段JSP代码:

<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>

 两个表单,相同点

1、action相同,都是 /testWeb/?query-param1=value1&query-param2=value2

2、表单域相同,都包含两个域:

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

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

两个表单,不同点:

表单1:   GET

表单2:POST

实验操作:

1、访问该JSP页面:http://localhost:8080/testWeb/index.jsp

2、GET提交表单1,提交后,地址栏变更为:

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

后端接收到的参数(request.getParameterNames()):

form-param1:value1 

form-param2:value2 

3、POST提交表单2,提交后,地址栏变更为:

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

后端接收到的参数(request.getParameterNames()):

query-param1:value1 

query-param2:value2 

form-param1:value1 

form-param2:value2 

3、修改表单2内容如下(query参数与form参数都新增加了参数aa,只是值不同):

<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>

再次提交表单2,地址栏变更为:

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

后端接收到的参数:

query-param1:value1 

query-param2:value2 

aa:cc, bb 

form-param1:value1 

form-param2:value2 

总结:

1、GET提交表单时,action地址中的query参数会被抛弃掉,后端取不到(即使是个空form,即不含任何输入域的form)。

2、POST提交表单时,action地址中的query参数不会被抛弃,会同form参数一同提交给后端。

3、POST提交表单时,如果action地址中的query参数存在与form参数同名的情况,也会一同提交给后端,后端request.getParameterValues(parameterName)得到的数组中会包含多个值。

4、request.getParameter()取得的参数不分query参数与form参数,也不分get参数与post参数。

5、如果只想获取query参数,可以通过request.getQueryString()获取查询字符串,然后自己分隔。

6、后端通过request.getParameterNames()获取参数时,地址栏query参数在前,POST form参数在后。

猜你喜欢

转载自huangqiqing123.iteye.com/blog/2386999