1. 程式人生 > >post和get對引數的異同

post和get對引數的異同

1.起始頁,會把一個引數(param)傳到post,get頁面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"
>
<title>Insert title here</title> </head> <body> <a href="get.jsp?param=a">get</a> <a href="post.jsp?param=a">post</a> </body> </html>

2.post,get頁面,大部分都一樣只是form的method不同而已

2.1get

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form
method="get" action="result.jsp?param=<%=request.getParameter("param") %>
"> <p> <label>param</label> <input type="text" name="param" value=""/> </p> <p> <label>other</label> <input type="text" name="other" value=""/> </p> <p> <input type="submit" name="get" value="get action"/> </p> </form> </body> </html>

2.2 post:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="result.jsp?param=<%=request.getParameter("param") %>">
<p>
    <label>param</label>
    <input type="text" name="param" value=""/>
</p>
<p>
    <label>other</label>
    <input type="text" name="other" value=""/>
</p>
<p>

    <input type="submit" name="post" value="post action"/>
</p>
</form>
</body>
</html>

3.result顯示結果

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
Map<String,String[]> param=request.getParameterMap();
Set<String> keys=param.keySet();
for(String k:keys){
    out.print("<p>key:"+k+",values:"+Arrays.toString(param.get(k))+"</p>");
}
%>
</body>
</html>

4.結果,表單中輸入param=b,other=c
這裡寫圖片描述