一、get请求方式
当通过get请求方式向服务器提交信息时,要提交的信息将会附加到url地址的后面。
例:
请求头:GET/servlet/ParamsServlet?param1=abc¶m2=xyz HTTP/1.1
是向servlet/ParamsServlet提交信息而信息的内容是param1=abc,param2=xyz。附加的信息用?后表示。而信息之间用&符号间隔。
特点:传送的数据量是有限制的,一般限制在1KB以下,并且存在安全隐患。当要提交用户名和密码时这就很不安全。
代码示例:
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="servlet/login" method="get" name="login"><!--这个登录表单使用的是get方式向服务器提交信息,提交后url会变成这样servlet/login?username=xx&password=xx-->
<input type="hidden" value="login" name="hidden1">
用户名:<input type="text" name="username"><br><br>
密 码:<input type="password" name="password"><br><br>
<input type="submit" value="登陆">
<a href="zhuce.html"><font size="1">没有账号注册</font></a>
</form>
</body>
</html>
二、post提交方式
post提交的信息不会增加在url的后面而是将附加在http请求里面。
例:
POST /servlet/ParamsServletHTTP/1.1
Host:
Content-Type: application/x-www-form-urlencoded
Content-Length: 28
param1=abc¶m2=xyz
特点:传送的数据量要比GET方式传送的数据量大得多。
把上面的:
<form action="servlet/login" method="post" name="login">换成<form action="servlet/login" method="post" name="login">
则url中不会显示附加的信息。附加的信息用户是看不到的这就保证了用户的安全,虽然可以通过httpwatch等类似的工具能获取http请求但我们可以通过一些加密算法将信息加密。加密后http请求就不会显示
POST /servlet/ParamsServletHTTP/1.1
Host:
Content-Type: application/x-www-form-urlencoded
Content-Length: 28
param1=abc¶m2=xyz
类似的param1=abc¶m2=xyz暴露信息的信息而是复杂的信息。这样就起到了保密的作用。