在上一篇博客中已经写好的html代码中加入 action 和 method
action:表示将表单中信息提交到哪个地址、
method:表示提交的方式,默认使用get,还有一种方式是post。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>reg</title>
</head>
<body>
<form action="http://127.0.0.1:9090" method="post">
<table border="1" bordercolor="#00ffff" cellpadding=10 cellspacing=0 width=600>
<tr>
<th colspan="2">注册表单:</th>
</tr>
<tr>
<td>用户名称:</td>
<td><input type="text" name="user"/></td>
</tr>
<tr>
<td>输入密码:</td>
<td><input type="password" name="psw"/></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="repsw"/></td>
</tr>
<tr>
<td>选择性别:</td>
<td>
<input type="radio" name="sex" value="nan"/>男
<input type="radio" name="sex" value="nv"/>女
</td>
</tr>
<tr>
<td>选择技术:</td>
<td>
<input type="checkbox" name="tech" value="java"/>JAVA
<input type="checkbox" name="tech" value="html"/>HTML
<input type="checkbox" name="tech" value="css"/>CSS
</td>
</tr>
<tr>
<td>选择国家:</td>
<td>
<select name="country">
<option value="none">---选择国家---</option>
<option value="use">美国</option>
<option value="en">英国</option>
<option value="cn">中国</option>
</select>
</td>
</tr>
<tr>
<th colspan="2">
<input type="reset" value="清除数据">
<input type="submit" value="提交数据">
</th>
</tr>
</table>
</form>
</body>
</html>
写一个服务端接收并打印提交过来的表单信息:
package com.mari.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class RegServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9090);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf, 0, len, "utf-8"));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("<font color='green' size='8'>注册成功</font>");
s.close();
ss.close();
}
}
get方法提交打印的信息:
127.0.0.1
GET /?user=aa&psw=aa&repsw=aa&sex=nan&tech=java&tech=html&country=cn HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)
Host: 127.0.0.1:9090
Connection: Keep-Alive
post方法提交打印的信息:
127.0.0.1
POST / HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
Content-Type: application/x-www-form-urlencoded
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)
Host: 127.0.0.1:9090
Content-Length: 62
Connection: Keep-Alive
Cache-Control: no-cache
user=aa&psw=aa&repsw=aa&sex=nan&tech=java&tech=html&country=cn
从控制台输出的信息我们就可以总结出get和post方法提交在客户端的区别:
- 一:
- get提交,提交的信息都显示在地址栏中。
- post提交,提交的信息不显示在地址栏中。
- 二:
- get提交,对于敏感的数据信息不安全。
- post提交,对于敏感的信息安装。
- 三:
- get提交,对于大数据不行,因为地址栏存储体积有限。
- post提交 ,可以提交大体积数据。
- 四:
- get提交,将数据封装到了请求消息的请求行中。
- post 提交,将信息封装到了请求体中。