http常见的form表单请求方式

      在Web开发中,我们使用的比较多的HTTP请求方式基本上就是GET、POST。

一、http请求常见的表单文件上传形式

     首先了解下application/x-www-form-urlencoded和multipart/form-data的区别:
  • application/x-www-form-urlencoded:是常用的表单发包方式,普通的表单提交,或者js发包,默认都是通过这种方式。
<form enctype="application/x-www-form-urlencoded" action="http://" method="POST">
    <input type="text" name="name" value="homeway">
    <input type="text" name="key" value="nokey">
    <input type="submit" value="submit">
</form

      当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url。
     当action为post时候,浏览器把form数据封装到http body中,然后发送到server(服务器)。

  • multipart/form-data
    如果没有 type=file 的控件,form表单会自动form的enctype属性为编码方式默认的 application/x-www-form-urlencoded
    如果有 type=file 的话,就要用到 multipart/form-data 了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file)、Content-Type(默认为text/plain)、name(控件name)等信息,并加上分割符(boundary)。

二、更加详细的Form表单请求

   Form表单请求,一般常用的是Get和Post提交方式,

  • Get方式提交
    表单内容
<form action="user/login.do" method="get" >  
    用户名:<input type="text" name="username"><br>  
    密码:<input type="text" name="password"><br>  
    <input type="submit" value="登录"/>  
</form>  

   Get方式提交,最后以http://localhost:8080/springmvc/user/login.do?username=xiaoming&password=123456789 请求服务器。

  • Post方式提交

   Post提交方式,Form表单有两种enctype类型:

   1、enctype=”application/x-www-form-urlencoded”

   也是默认的提交类型,一般针对文本请求参数,不含附件。比如

<form action="user/login.do" method="post" >  
    用户名:<input type="text" name="username"><br>  
    密码:<input type="text" name="password"><br>  
    <input type="submit" value="登录"/>  
</form>  

   提交表单时的Http请求如下:

POST http://localhost:8080/springmvc/user/login.do HTTP/1.1  
Host: localhost:8080  
Connection: keep-alive  
Content-Length: 33  
Cache-Control: max-age=0  
Origin: http://localhost:8080  
Upgrade-Insecure-Requests: 1  
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36  
Content-Type: application/x-www-form-urlencoded  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8  
Referer: http://localhost:8080/springmvc/  
Accept-Encoding: gzip, deflate  
Accept-Language: zh-CN,zh;q=0.8  

username=xiaoming&password=123456789  

   消息头中的Content-Type: application/x-www-form-urlencoded
   消息体中内容以key=value的形式拼接
username=xiaoming&password=123456789
   2、enctype=”multipart/form-data”

   需要上传附件时,必须为”multipart/form-data”。**,比如

<form action="user/login.do" method="post" enctype="multipart/form-data">  
    用户名:<input type="text" name="username"><br>  
    密码:<input type="text" name="password"><br>  
    上传文件:<input type="file" name="uploadFile"/><br>  
    <input type="submit" value="登录"/>  
</form>  

   提交表单时,Http请求协议如下:

POST http://localhost:8080/springmvc/user/login.do HTTP/1.1  
Host: localhost:8080  
Connection: keep-alive  
Content-Length: 400  
Cache-Control: max-age=0  
Origin: http://localhost:8080  
Upgrade-Insecure-Requests: 1  
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36  
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarykALcKBgBaI9xA79y  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8  
Referer: http://localhost:8080/springmvc/  
Accept-Encoding: gzip, deflate  
Accept-Language: zh-CN,zh;q=0.8  

------WebKitFormBoundarykALcKBgBaI9xA79y  
Content-Disposition: form-data; name="username"  

xiaoming 
------WebKitFormBoundarykALcKBgBaI9xA79y  
Content-Disposition: form-data; name="password"  

123456789  
------WebKitFormBoundarykALcKBgBaI9xA79y  
Content-Disposition: form-data; name="uploadFile"; filename="file.txt"  
Content-Type: text/plain  

文件中的内容       
------WebKitFormBoundarykALcKBgBaI9xA79y--  

   请求消息头中, Content-Type: multipart/form-data; boundary=- - - -WebKitFormBoundarykALcKBgBaI9xA79y
boundary为分隔符.
   消息体中的每个参数都会以“- -”+boundary 隔开,最后一个分隔符末尾需要加”- -“,即”- -“+boundary+”- -“

   更加详细的讲解请参考
   http://blog.csdn.net/bboyfeiyu/article/details/41863951
   http://blog.csdn.net/zhangquanit/article/details/52471998

  • 9
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
在 HTML 页面中,你可以使用 `<form>` 元素来创建表单,使用 `method="post"` 属性来指定表单提交的方式为 POST。你可以在表单中添加各种表单控件,例如输入框、下拉菜单、单选框等等。当用户在表单中填写完数据后,点击提交按钮,表单数据就会以 POST 方式提交到指定的 URL。 以下是一个简单的示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>POST 请求示例</title> </head> <body> <form method="post" action="http://example.com/handle_post_request"> <label for="username">用户名:</label> <input type="text" id="username" name="username"><br><br> <label for="password">密码:</label> <input type="password" id="password" name="password"><br><br> <input type="submit" value="提交"> </form> </body> </html> ``` 在上面的示例中,表单的提交方式为 POST,提交的 URL 为 `http://example.com/handle_post_request`。表单中包含了两个输入框:一个用户名输入框和一个密码输入框。当用户填写完数据后,点击提交按钮,表单数据就会以 POST 方式提交到 `http://example.com/handle_post_request`。 在 Servlet 中,你可以通过 `doPost()` 方法来处理 POST 请求,例如: ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取表单数据 String username = request.getParameter("username"); String password = request.getParameter("password"); // 处理表单数据 // ... // 返回响应 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.write("处理结果"); out.flush(); out.close(); } ``` 在上面的示例中,`doPost()` 方法中获取了表单数据,然后处理表单数据,并返回了处理结果。在响应中,设置了响应的内容类型和字符编码,然后向客户端输出了处理结果。 注意:在 Servlet 中,需要在 `web.xml` 文件中对 Servlet 进行配置,以便能够正确地处理 POST 请求。例如: ```xml <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.example.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/handle_post_request</url-pattern> </servlet-mapping> ``` 在上面的示例中,我们定义了一个名为 `MyServlet` 的 Servlet,然后将它映射到 URL `/handle_post_request` 上。这样,当浏览器提交 POST 请求到 `/handle_post_request` 时,就会自动调用 `MyServlet` 的 `doPost()` 方法来处理请求

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值