【示例程序】
<html>
<form action="http://114.230.97.70:9090" method="get">
<table border="1" bordercolor="#0000ff" 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 name="none">--选择国家--</option>
<option name="usa">美国</option>
<option name="en">英国</option>
<option name="cn">中国</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="reset" value="清除数据">
<input type="submit" value="提交数据">
</td>
</tr>
</table>
</form>
</html>
<form action="http://114.230.97.70:9090" method="get">这行代码意思是连接服务器114.230.97.70:9090,其中9090是端口号,
method="get"是“连接方式”(或“提交数据方式”),这里还可以改为post方式提交(即method="post")
【效果】
网页表单数据填写完毕后,单击“提交数据”,服务端收到注册信息(需要事先开启服务端,服务端代码见Server.class)
【重点-GET提交和POST提交的区别】
(1)get提交,提交的信息都显示在地址栏中;post提交,提交的信息不显示在地址栏中;
(2)get提交,对于敏感的数据信息不安全;post提交,对于敏感信息安全;
(比如密码,get提交在地址栏能见到密码,而post提交见不到)
(3)get提交,对于大数据不行,因为地址栏存储体积有限;post提交,可以提交大体积数据;
(4)get提交,将信息封装到了请求消息的请求行中;post提交,将信息封装到了请求体中。
/*
* 由于百度拒收提交数据,所以自己做个服务端,用于接收注册数据。
*/
package demo;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
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));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("<font color = 'green' size='7'>注册成功</font>");
s.close();
ss.close();
}
}