一 HTTP簡介
Hyper Text Transfer Protocol 超文本傳輸協議
定義了客戶端和服務器通信時 發送資料的格式
二 HTTP特點
1. 基于TCP/IP的高級協議
2. 默認埠號80
3. 基于請求/回應模型 一次請求對應一次回應
4. 無狀態的 每次請求之間相互獨立 不能互動資料
三 請求訊息資料格式
原始資料
POST /login.html HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://localhost/login.html
Connection: keep-alive
Upgrade-Insecure-Requests: 1
username=zhangsan
1. 請求行
GET /login.html HTTP/1.1 -> 請求方式 請求url 請求協議/版本
HTTP協議有7種請求方式 常用的有2種
GET
a. 請求引數在請求行中 拼接在url后面
b. 請求的url長度有限制
c. 不太安全
POST
a. 請求引數在請求體中
b. 請求的url長度沒有限制
c. 相對安全
2. 請求頭
客戶端告訴服務器一些資訊 固定格式(請求頭名稱: 請求頭值)
常見的請求頭
a. User-Agent 當前使用的版本資訊
b. Referer 當前請求從哪里來
3. 請求空行
空行 用于分割POST請求的請求頭和請求體
4. 請求體(正文)
封裝POST請求的請求引數
四 Request|Response執行原理
1. 服務器會根據請求url中的資源路徑 創建對應的servlet物件
2. 服務器會創建request物件和response物件 request物件中封裝請求訊息資料
3. 服務器將request物件和response物件 傳遞給service() 并且呼叫service()
4. 程式員可以通過request物件獲取請求訊息資料 通過response物件設定回應訊息資料
5. 服務器在給客戶端做出回應之前 會從response物件中拿程式員設定的回應訊息資料
五 Request功能
1. 獲取請求訊息資料
a. 獲取請求行資料
請求行原始資料
GET /xxx/demo1?name=zhangsan HTTP/1.1
獲取請求方式 String getMethod() -> GET
獲取虛擬目錄 String getContextPath() -> /xxx
獲取servlet路徑 String getServletPath() -> /demo1
獲取get方式請求引數 String getQueryString() -> name=zhangsan
獲取請求uri String getRequestURI() -> /xxx/demo1
獲取請求url StringBuffer getRequestURL() -> http://localhost/xxx/demo1
獲取協議及版本 String getProtocol() -> HTTP/1.1
獲取客戶端IP地址 String getRemoteAddr() -> 0:0:0:0:0:0:0:1
b. 獲取請求頭資料
獲取所有請求頭名稱 Enumeration<String> getHeaderNames()
通過請求頭名稱獲取請求頭值 String getHeader(String name)
c. 獲取請求體資料
請求體只有post方式請求才有 封裝了請求引數
1> 獲取流物件
BufferedReader getReader() -> 獲取字符輸入流 只能操作字符型別資料
ServletInputStream getInputStream() -> 獲取位元組輸入流 可以操作所有型別資料
2> 從流物件中拿資料
2. 其他功能
a. 獲取請求引數通用方式
get和post都可以使用下列方法來獲取請求引數
通過引數名稱獲取引數值 String getParameter(String name)
通過引數名稱獲取引數值的陣列 String[] getParameterValues(String name)
獲取所有引數名稱 Enumeration<String> getParameterNames()
獲取所有引數名稱和引數值的陣列 Map<String, String[]> getParameterMap()
b. 請求轉發
一種在服務器內部的資源跳轉方式
特點
1> 瀏覽器地址欄路徑不發生變化
2> 只能轉發到當前服務器內部資源中
3> 轉發是一次請求
步驟
ServletRequest#getRequestDispatcher(String path).forward(ServletRequest request, ServletResponse response)
c. 共享資料
域物件: 一個有作用范圍的物件 可以在范圍內共享資料
Request域: 代表一次請求的范圍 一般用于請求轉發的多個資源中共享資料
存盤資料 void setAttribute(String name, Object obj)
通過鍵獲取值 Object getAttribute(String name)
通過鍵移除值 void removeAttribute(String name)
d. 獲取ServletContext
ServletContext getServletContext()
六 ServletContext
代表整個web應用 可以和程式的容器(服務器)來通信
1. 獲取
a. 通過ServletRequest獲取 ServletRequest#getServletContext()
b. 通過GenericServlet獲取 GenericServlet#getServletContext()
2. 功能
a. 獲取MIME型別
MIME型別: 在互聯網通信程序中定義的一種檔案資料型別 固定格式(大型別/小型別) 比如text/html image/jpeg
獲取: String getMimeType(String file)
b. 共享資料
域物件: 一個有作用范圍的物件 可以在范圍內共享資料
ServletContext域: 代表整個web應用
存盤資料 void setAttribute(String name, Object obj)
通過鍵獲取值 Object getAttribute(String name)
通過鍵移除值 void removeAttribute(String name)
c. 獲取檔案在服務器上的真實路徑
ServletContext#getRealPath("/b.txt") -> web目錄下資源訪問
ServletContext#getRealPath("/WEB-INF/c.txt") -> WEB-INF目錄下資源訪問
ServletContext#getRealPath("/WEB-INF/classes/a.txt") -> src目錄下資源訪問
七 回應訊息資料格式
原始資料
HTTP/1.1 200 OK Content-Type: text/html;charset=UTF-8 Content-Length: 101 Date: Wed, 06 Jun 2018 07:08:42 GMT <html> <head> <title>$Title$</title> </head> <body> hello response </body> </html>
1. 回應行
HTTP/1.1 200 OK -> 回應協議/版本 回應狀態碼 回應狀態碼描述
回應狀態碼: 服務器告訴客戶端本次請求和回應的一個狀態 狀態碼都是3位數字
1xx 服務器接收客戶端訊息 但沒有接收完成 等待一段時間后 發送1xx狀態碼
2xx 成功 比如200
3xx 重定向 比如重定向302 訪問快取304
4xx 客戶端錯誤 比如請求路徑沒有對應的資源404 請求方式沒有對應的doXxx方法405
5xx 服務器端錯誤 比如服務器內部出現例外500
2. 回應頭
服務器告訴客戶端一些資訊 固定格式(回應頭名稱: 回應頭值)
常見的回應頭
a. Content-Type 服務器告訴客戶端本次回應體資料格式以及編碼格式
b. Content-Disposition 服務器告訴客戶端以什么格式打開回應體資料
in-line 默認值 在當前頁面內打開
attachment;filename=xxx 以附件形式打開回應體(檔案下載)
3. 回應空行
空行 用于分割回應頭和回應體
4. 回應體
傳輸的資料
八 Response功能
1. 設定回應訊息資料
a. 設定回應行資料
設定狀態碼 void setStatus(int code)
b. 設定回應頭資料
設定回應頭 void setHeader(String name, String value)
c. 設定回應體資料
1> 獲取流物件
PrintWriter getWriter() -> 獲取字符輸出流 只能操作字符型別資料
ServletOutputStream getOutputStream() -> 獲取位元組輸出流 可以操作所有型別資料
2> 使用輸出流 將資料輸出到客戶端
2. 其他功能
a. 重定向
資源跳轉方式
特點
1> 瀏覽器地址欄路徑發生變化
2> 可以訪問其他服務器資源
3> 重定向是兩次請求
步驟
HttpServletResponse#sendRedirect(String path)
九 中文亂碼
1. 請求引數
get -> tomcat8已經將亂碼問題解決了
post -> 需要在獲取引數之前 設定request的編碼 ServletRequest#setCharacterEncoding("utf-8")
2. 回應資料
需要在獲取流之前 設定response的編碼 ServletResponse#setContentType("text/html;charset=utf-8")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/152376.html
標籤:Java
