Http協議相關知識
1 基礎知識
web入門
1)web服務軟體作用: 把本地資源共享給外部訪問
2)tomcat伺服器基本操作 :
啟動: %tomcat%/bin/startup.bat
關閉: %tomcat%/bin/shutdown.bat
訪問tomcat主頁:
http://localhost:8080
3)web應用目錄結構
|-WebRoot 根目錄
|-靜態資源(html+css+javascript+images+xml) 可以直接被瀏覽器訪問到的
|-WEB-INF 不可以直接被瀏覽器訪問到
|-classes 存放class位元組碼檔案
|-lib 存放jar包檔案
web.xml web應用的配置檔案,配置servlet
4)Servlet技術: 用java語言開發動態資源的技術
開發一個Servlet程式的步驟:
1)建立一個java類,繼承HttpServlet類
2)重寫HttpServlet類的doGet方法
3)把寫好的servlet程式交給tomcat伺服器執行!!!!
3.1把編譯好的servlet的class檔案拷貝到tomcat的一個web應用中。(web應用 的WEB-INF/classes目錄下)
3.2在當前web應用的web.xml檔案中配置servlet
<!--servlet配置 -->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>gz.itcast.HelloServlet</servlet-class>
</servlet>
<!-- servlet的對映配置 -->
<servlet-mapping>
<servlet-name>HelloServlet </servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
4)訪問servlet
http://localhost:8080/myweb/hello
2.1 什麼是http協議
http協議:對瀏覽器客戶端 和 伺服器端 之間資料傳輸的格式規範
2.2 檢視http協議的工具
1)使用火狐的firebug外掛(右鍵->firebug->網路)
2)使用谷歌的“審查元素”
3)使用系統自帶的telnet工具(遠端訪問工具)
a)telnet localhost 8080 訪問tomcat伺服器
b)ctrl+] 回車 可以看到回顯
c)輸入請求內容
GET /day09/hello HTTP/1.1 Host: localhost:8080 |
d)回車,即可檢視到伺服器響應資訊。
2.3http協議內容
請求(瀏覽器-》伺服器) GET /day09/hello 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 Connection: keep-alive |
響應(伺服器-》瀏覽器) 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!!! |
3 Http請求
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 --(可選)實體內容 |
3.1 請求行
GET/day09/hello HTTP/1.1
#http協議版本
http1.0:當前瀏覽器客戶端與伺服器端建立連線之後,只能傳送一次請求,一次請求之後連線關閉。
http1.1:當前瀏覽器客戶端與伺服器端建立連線之後,可以在一次連線中傳送多次請求。(基本都使用1.1)
#請求資源
URL: 統一資源定位符。http://localhost:8080/day09/testImg.html。只能定位網際網路資源。是URI 的子集。
URI:統一資源標記符。/day09/hello。用於標記任何資源。可以是本地檔案系統,區域網的資源(//192.168.14.10/myweb/index.html), 可以是網際網路。
#請求方式
常見的請求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE
常用的請求方式: GET 和 POST
表單提交:
<formaction="提交地址"method="GET/POST">
<form>
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後面。引數而是跟在請求的實體內容中。沒有?開頭,多個引數之間以&分割。
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 |
b)POST提交的引數資料沒有限制。
c)POST方式提交敏感資料。
3.2 請求頭
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 -- 請求發出的時間 |
3.3 實體內容
只有POST提交的引數會放到實體內容中
3.4HttpServletRequest物件
HttpServletRequest物件作用是用於獲取請求資料。
核心的API:
請求行:
request.getMethod(); 請求方式
request.getRequetURI() / request.getRequetURL() 請求資源
request.getProtocol() 請求http協議版本
請求頭:
request.getHeader("名稱") 根據請求頭獲取請求值
request.getHeaderNames() 獲取所有的請求頭名稱
實體內容:
request.getInputStream() 獲取實體內容資料
3.5service 和 doXX方法區別
HttpSevlet類的原始碼: protectedvoid service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //得到請求方式 String method = req.getMethod();
if (method.equals(METHOD_GET)) { long lastModified = getLastModified(req); if (lastModified == -1) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic doGet(req, resp); } else { long ifModifiedSince; try { ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); } catch (IllegalArgumentException iae) { // Invalid date header - proceed as if none was set ifModifiedSince = -1; } if (ifModifiedSince < (lastModified / 1000 * 1000)) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } }
} elseif (method.equals(METHOD_HEAD)) { long lastModified = getLastModified(req); maybeSetLastModified(resp, lastModified); doHead(req, resp);
} elseif (method.equals(METHOD_POST)) { doPost(req, resp);
} elseif (method.equals(METHOD_PUT)) { doPut(req, resp);
} elseif (method.equals(METHOD_DELETE)) { doDelete(req, resp);
} elseif (method.equals(METHOD_OPTIONS)) { doOptions(req,resp);
} elseif (method.equals(METHOD_TRACE)) { doTrace(req,resp);
} else { // // Note that this means NO servlet supports whatever // method was requested, anywhere on this server. //
String errMsg = lStrings.getString("http.method_not_implemented"); Object[] errArgs = new Object[1]; errArgs[0] = method; errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); } } |
3.6 案例-獲取瀏覽器的型別(user-agent)
3.7 案例- 防止非法連結(referer)
第1次 CSDN/51CTO -> 頁面(點選下載) -> 彈出廣告頁面(點選此處下載) -> 開始下載
第2次 直接點選此處下載 -> 轉回廣告頁面 -> 開始下載
非法連結:
1)直接訪問下載的資源
2)不是從廣告頁面過來的連結
referer:當前請求來自於哪裡。
3.8 傳遞的請求引數如何獲取
GET方式: 引數放在URI後面
POST方式: 引數放在實體內容中
獲取GET方式引數:
request.getQueryString();
獲取POST方式引數:
request.getInputStream();
問題:但是以上兩種不通用,而且獲取到的引數還需要進一步地解析。
所以可以使用統一方便的獲取引數的方式:
核心的API:
request.getParameter("引數名"); 根據引數名獲取引數值(注意,只能獲取一個值的引數)
request.getParameterValue("引數名“);根據引數名獲取引數值(可以獲取多個值的引數)
request.getParameterNames(); 獲取所有引數名稱列表
3.9 請求引數編碼問題
修改POST方式引數編碼:
request.setCharacterEncoding("utf-8");
修改GET方式引數編碼:
手動解碼:String name = newString(name.getBytes("iso-8859-1"),"utf-8");
4 Http響應
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!!! --實體內容 |
4.1響應行
#http協議版本
#狀態碼: 伺服器處理請求的結果(狀態)
常見的狀態:
200: 表示請求處理完成並完美返回
302: 表示請求需要進一步細化。
404: 表示客戶訪問的資源找不到。
500: 表示伺服器的資源傳送錯誤。(伺服器內部錯誤)
#狀態描述
4.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:儲存連線 |
4.3HttpServletResponse物件
HttpServletResponse物件修改響應資訊:
響應行:
response.setStatus() 設定狀態碼
響應頭:
response.setHeader("name","value") 設定響應頭
實體內容:
response.getWriter().writer(); 傳送字元實體內容
response.getOutputStream().writer() 傳送位元組實體內容