< 笔记 > HTML - 03 HTML 表单提交

03 HTML 表单提交

By Kevin Song

GET提交和POST提交的区别

  • 区别一
    • GET提交,提交的信息显示在地址栏中
    • POST提交,提交的信息不显示地址栏中
  • 区别二
    • GET提交,敏感数据信息不安全,密码暴露无遗
    • POST提交,敏感数据安全
  • 区别三
    • GET提交,对于大数据不行,地址栏存储体积有限
    • POST提交,可以提交大体积数据
  • 区别四
    • GET提交,把信息封装到了请求消息的请求行中
    • POST提交,把信息封装到了请求消息的请求体中
  • 区别五(在服务端)当中文提交到Tomcat服务器,服务器会默认用ISO8859-1进行解码出现乱码。用IOS8859-1进行编码,再用指定中文码表解码即可。这种方式对GET提交和POST提交都有效
    • 对于POST提交的中文,还有另一种解决方法,直接使用服务端的一个对象。
    • request对象的setCharacterEncoding方法直接设置指定的中文码表就可以将中文数据解析出来
    • 该方法只对请求体中的数据进行解码

前端和服务端交互的三种方式

  1. 地址栏输入url地址—GET请求
  2. 超链接—GET请求
  3. 表单—GET和POST请求

增强型校验

增强型校验定义:只要有一个组件内容错误,是无法继续提交的只有全对才可以提交

客户端进行增强型校验后,服务端也需要进行校验
原因:
增加安全性,因为与服务端交互方式不止表单提交一种,可以防止直接在地址栏输入url地址暴力注册,确保服务端只会收到正确的信息。

服务端进行增强型校验后,客户端也需要进行校验
原因:
增加用户上网体验,每次都在服务端校验增加了等待时间,并且增加了服务端的压力

前端代码:

<html>
    <head>
        <title>Kevin's Homepage</title>
    </head>
    <body>
        <form action="http://10.1.31.69:9090" method="get">
            <table border="1" bordercolor="#00ffff">
                <tr>
                    <thcolspan="2">注册表单</th>
                </tr>
                <tr>
                    <td>用户名称</td>
                    <td><input type="text" name="user" value="" /></td>
                </tr>
                <tr>
                    <td>输入密码</td>
                    <td><input type="password" name="pwd"/></td>
                </tr>
                <tr>
                    <td>确认密码</td>
                    <td><input type="password" name="repwd"/></td>
                </tr>
                <tr>
                    <td>选择性别</td>
                    <td>
                    <input type="radio" name="gender" value="man" /><input type="radio" name="gender" value="woman" /></td>
                </tr>
                <tr>
                    <td>选择技术</td>
                    <td>
                    <input type="checkbox" name="tech" value="Java"/>Java
                    <input type="checkbox" name="tech" value="C++"/>C++
                    <input type="checkbox" name="tech" value="Python"/>Python
                    </td>
                </tr>
                <tr>
                    <td>选择国家</td>
                    <td>
                        <select name="country">
                            <option value="none">--选择国家--</option>
                            <option value="USA">美国</option>
                            <option value="CN" selected="selected">中国</option>
                            <option value="KR">韩国</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th colspan="2">
                    <input type="reset" value="重置表单">
                    <input type="submit" value="提交表单">
                    </th>
                </tr>
            </table>
        </form>
    </body>
</html>

服务端代码

public class RegServer {
    public static void main(String[] args) {
        ServerSocket ss = new ServerSocket(9090);
        Socket s = ss.accept();
        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();
    }
}

提交方式:GET
地址栏:http://10.1.31.69:9090/?user=abc&psw=123&repsw=123&sex=nan&tech=java&tech=html&country=cn

GET /?user=abc&psw=123&repsw=123&sex=nan&tech=java&tech=html&country=cn HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-cn,zu;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2)
Host: 10.1.31.69:9090
Connection: Keep-Alive

提交方式:POST
地址栏:http://10.1.31.69:9090/

POST / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-cn,zu;q=0.5
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2)
Host: 10.1.31.69:9090
Content-Length: 68
Connection: Keep-Alive
Cache-Control: no-cache

user=hahah&psw=8989&repsw=8989&sex=nv&tech=html&tech=css&country=usa
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章