一.請求報文: html
1.http協議內容:java
GET /day09/hello HTTP/1.1 -請求行 Host: localhost:8080 --請求頭(多個key-value對象) User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive --一個空行 name=eric&password=123456 --(可選)實體內容
響應(服務器-》瀏覽器) HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Length: 24 Date: Fri, 30 Jan 2015 01:54:57 GMT this is hello servlet!!!
http1.0:當前瀏覽器客戶端與服務器端創建鏈接以後,只能發送一次請求,一次請求以後鏈接關閉。web
http1.1:當前瀏覽器客戶端與服務器端創建鏈接以後,能夠在一次鏈接中發送屢次請求。(基本都使用1.1)apache
3.請求資源瀏覽器
URL: 統一資源定位符。http://localhost:8080/day09/testImg.html。只能定位互聯網資源。是URI 的子集。緩存
URI: 統一資源標記符。/day09/hello。用於標記任何資源。能夠是本地文件系統,局域網的資源(//192.168.14.10/myweb/index.html), 能夠是互聯網。tomcat
4.請求方式服務器
常見的請求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETEcookie
經常使用的請求方式: GET 和 POSTapp
表單提交:
<form action="提交地址" method="GET/POST">
<form>
5.GET vs POST 區別
1)GET方式提交
a)地址欄(URI)會跟上參數數據。以?開頭,多個參數之間以&分割。
GET /day09/testMethod.html?name=eric&password=123456 HTTP/1.1
Host: localhost:8080 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Referer: http://localhost:8080/day09/testMethod.html Connection: keep-alive
b)GET提交參數數據有限制,不超過1KB。
c)GET方式不適合提交敏感密碼。
d)注意: 瀏覽器直接訪問的請求,默認提交方式是GET方式
2)POST方式提交
a)參數不會跟着URI後面。參數而是跟在請求的實體內容中。沒有?開頭,多個參數之間以&分割。
b)POST提交的參數數據沒有限制。
c)POST方式提交敏感數據。
POST /day09/testMethod.html HTTP/1.1 Host: localhost:8080 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Referer: http://localhost:8080/day09/testMethod.html Connection: keep-alive name=eric&password=123456
6.請求頭
Accept: text/html,image/* -- 瀏覽器接受的數據類型 Accept-Charset: ISO-8859-1 -- 瀏覽器接受的編碼格式 Accept-Encoding: gzip,compress --瀏覽器接受的數據壓縮格式 Accept-Language: en-us,zh- --瀏覽器接受的語言 Host: www.it315.org:80 --(必須的)當前請求訪問的目標地址(主機:端口) If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT --瀏覽器最後的緩存時間 Referer: http://www.it315.org/index.jsp -- 當前請求來自於哪裏 User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) --瀏覽器類型 Cookie:name=eric -- 瀏覽器保存的cookie信息 Connection: close/Keep-Alive -- 瀏覽器跟服務器鏈接狀態。close: 鏈接關閉 keep-alive:保存鏈接。 Date: Tue, 11 Jul 2000 18:23:51 GMT -- 請求發出的時間
只有POST提交的參數會放到實體內容中
HttpServletRequest對象做用是用於獲取請求數據。
核心的API:
請求行:
request.getMethod(); 請求方式
request.getRequetURI() / request.getRequetURL() 請求資源
request.getProtocol() 請求http協議版本
請求頭:
request.getHeader("名稱") 根據請求頭獲取請求值
request.getHeaderNames() 獲取全部的請求頭名稱
實體內容:
request.getInputStream() 獲取實體內容數據
9.傳遞的請求參數如何獲取
GET方式: 參數放在URI後面
POST方式: 參數放在實體內容中
獲取GET方式參數:
request.getQueryString();
獲取POST方式參數:
request.getInputStream();
問題:可是以上兩種不通用,並且獲取到的參數還須要進一步地解析。
因此能夠使用統一方便的獲取參數的方式:
核心的API:
request.getParameter("參數名"); 根據參數名獲取參數值(注意,只能獲取一個值的參數)
request.getParameterValue("參數名「);根據參數名獲取參數值(能夠獲取多個值的參數)
request.getParameterNames(); 獲取全部參數名稱列表
10.請求參數編碼問題
修改POST方式參數編碼:
request.setCharacterEncoding("utf-8");
修改GET方式參數編碼:
手動解碼:String name = new String(name.getBytes("iso-8859-1"),"utf-8");
11.請求報文的代碼練習:
package com.http.requst; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RequstExercise extends HttpServlet { /** * Constructor of the object. */ public RequstExercise() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gb2312"); // System.out.println("GET方式"); // String value = request.getQueryString(); // System.out.println(value); //t1(request); //t2(request); //--直接獲取指定名稱的值 /* String name = request.getParameter("user"); String pwd = request.getParameter("pwd"); System.out.println("用戶名: " + name); System.out.println("密碼: " + pwd); */ //訪問全部的 Enumeration<String> enums = request.getParameterNames(); while(enums.hasMoreElements()){ String name = enums.nextElement(); if("GET".equals(request.getMethod())){ name = new String(name.getBytes("iso-8859-1"), "gb2312"); } //System.out.println(name + " 是: " + request.getParameter(name));//不能處理多個值 System.out.print(name + "是: "); String[] values = request.getParameterValues(name); for(String value : values){ if("GET".equals(request.getMethod())){ value = new String(value.getBytes("iso-8859-1"), "gb2312"); } System.out.print(value + "\t"); } System.out.println(); } } private void t2(HttpServletRequest request) { System.out.println("Host : " + request.getHeader("Host")); Enumeration<String> enums = request.getHeaderNames(); while(enums.hasMoreElements()){ String headerName = enums.nextElement(); System.out.println(headerName + " : " + request.getHeader(headerName)); } } private void t1(HttpServletRequest request) { System.out.println("請求方式是:" + request.getMethod()); System.out.println("HTTP版本:" + request.getProtocol()); System.out.println("URI:" + request.getRequestURI()); System.out.println("URL" + request.getRequestURL()); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ServletInputStream in = request.getInputStream(); // byte[] buf = new byte[1024]; // int len = 0; // while( (len=in.read(buf))!=-1 ){ // String str = new String(buf, 0, len); // System.out.println(str); // } doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
二.響應報文
HTTP/1.1 200 OK --響應行 Server: Apache-Coyote/1.1 --響應頭(key-vaule) Content-Length: 24 Date: Fri, 30 Jan 2015 01:54:57 GMT --一個空行 this is hello servlet!!! --實體內容
1.狀態碼: 服務器處理請求的結果(狀態)
常見的狀態:
200 : 表示請求處理完成並完美返回
302: 表示請求須要進一步細化。 404: 表示客戶訪問的資源找不到。
500: 表示服務器的資源發送錯誤。(服務器內部錯誤)
2 常見的響應頭
Location: http://www.it315.org/index.jsp -表示重定向的地址,該頭和302的狀態碼一塊兒使用。 Server:apache tomcat ---表示服務器的類型 Content-Encoding: gzip -- 表示服務器發送給瀏覽器的數據壓縮類型 Content-Length: 80 --表示服務器發送給瀏覽器的數據長度 Content-Language: zh-cn --表示服務器支持的語言 Content-Type: text/html; charset=GB2312 --表示服務器發送給瀏覽器的數據類型及內容編碼 Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT --表示服務器資源的最後修改時間 Refresh: 1;url=http://www.it315.org --表示定時刷新 Content-Disposition: attachment; filename=aaa.zip --表示告訴瀏覽器如下載方式打開資源(下載文件時用到) Transfer-Encoding: chunked Set-Cookie:SS=Q0=5Lb_nQ; path=/search --表示服務器發送給瀏覽器的cookie信息(會話管理用到) Expires: -1 --表示通知瀏覽器不進行緩存 Cache-Control: no-cache Pragma: no-cache Connection: close/Keep-Alive --表示服務器和瀏覽器的鏈接狀態。close:關閉鏈接 keep-alive:保存鏈接
HttpServletResponse對象修改響應信息:
響應行:
response.setStatus() 設置狀態碼
響應頭:
response.setHeader("name","value") 設置響應頭
實體內容:
response.getWriter().writer(); 發送字符實體內容
response.getOutputStream().writer() 發送字節實體內容
7.響應報文代碼練習:
package com.http.response; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ResponseExercise extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("gb2312"); //func_response1(response); //refresh(response); //response.setHeader("content-type", "text/html"); //response.setContentType("text/xml"); //response.getWriter().write("<html><head><title>handsomcui</title></head><body>hello everyone</body></html>"); response.setContentType("image/jpg"); FileInputStream file = new FileInputStream(new File("E:/個人java程序/code/MyWeb/WebRoot/img/15.JPG")); int len; byte[] buf = new byte[1024]; while( (len = file.read(buf)) != -1){ response.getOutputStream().write(buf, 0, len); } file.close(); } private void refresh(HttpServletResponse response) throws IOException { response.setStatus(302); response.setHeader("location", "/MyWeb/adv.html"); response.sendRedirect("/MyWeb/adv.html"); response.setHeader("refresh", "1"); response.setHeader("refresh", "3;url=/MyWeb/adv.html"); } private void func_response1(HttpServletResponse response) throws IOException { response.setStatus(404); response.sendError(404); response.getWriter().write("I'm handsomecui"); response.getOutputStream().write("I'm a doubi".getBytes()); response.setHeader("server", "handsomecui"); response.setHeader("author", "cuigege"); response.setHeader("Date", "2006.1.1"); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
三.總結:
http協議: 瀏覽器和服務器之間數據傳輸的格式規範
1)http請求:
格式:
請求行
請求頭
空行
實體內容(POST提交的數據在實體內容中)
重點:
使用HttpServletRequest對象: 獲取請求數據
2)http響應;
格式:
響應行
響應頭
空行
實體內容(瀏覽器看到的內容)
重點:
使用HttpServletResponse對象: 設置響應數據