两者的比较:
Get方式:
将请求的参数名和值转换成字符串,并附加在原来的URL之后,不安全
传输的数据量较小,一般不能大于2KB;
post方式:
数量较大;
请求的参数和值放在HTML的请求头中,安全性较高。
实例1:
<%@ 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 id="form1" action="reuset1.jsp" method="post" >
用户名:<br/>
<input type="text" name="username">
<hr/>
性别:<br/>
男:<input type="radio" name="gender" value="男">
女:<input type="radio" name="gender" value="女">
<hr/>
喜欢的颜色:<br/>
红色:<input type="checkbox" name="color" value="红色">
绿色:<input type="checkbox" name="color" value="绿色">
蓝色:<input type="checkbox" name="color" value="蓝色">
<hr/>
来自的国家:<br/>
<select name="country">
<option value="中国"> 中国</option>
<option value="美国">美国</option>
<option value="俄罗斯">俄罗斯</option>
</select>
<hr/>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
显示如下:
处理表单
<%@ 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>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");
String[] color = request.getParameterValues("color");
%>
姓名:<%=name %>
<hr/>
喜欢的颜色:<%for(String c : color)
{out.println(c + "");}%>
</body>
</html>
结果
GET的中文乱码:
注意获取get的方式中文参数,比较复杂,需要借助于URLDecoder类进行转码,或者重新编码或解码。
使用重新编码的方式:
<%@ 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>
<%
String rawName = request.getParameter("username");
//将字符串使用ISO-8859-1分解成字节数组
byte[] rawBytes = rawName.getBytes("ISO-8859-1");
//将字节数组重新解码成字符串
String username = new String(rawBytes,"UTF-8");
%>
原始查询字符串:<%=rawName %><hr/>
重新编码解码的字符串:<%=username %>
</body>
</html>
结果:
使用URLDecoder类的方式:
<%@ 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>
<%
String rawQueryStr = request.getQueryString();
out.println("原始查询字符串:" + rawQueryStr + "<hr/>");
//使用URLDecoder解码字符串
String queryStr = java.net.URLDecoder.decode(rawQueryStr,"UTF-8");
out.println("解码后的字符串:" + queryStr + "<hr/>");
//以&分解查询字符串
String[] paramPairs = queryStr.split("&");
for (String paramPair : paramPairs){
out.println("请求参数的键值对为:" + paramPair + "<br/>");
String[] nameValue = paramPair.split("=");
out.println(nameValue[0] + "参数值是: " + nameValue[1] + "<hr/>");
}
%>
</body>
</html>
结果: