虛擬存儲涉及到的相關基礎知識總結 1
690
2025-04-02
文章目錄
HTTP概念:
特點:
歷史版本:
請求消息數據格式
1. 請求行
2. 請求頭:客戶端瀏覽器告訴服務器一些信息
3. 請求空行
4. 請求體(正文):
Request
HTTP概念:
Hyper Text Transfer Protocol 超文本傳輸協議
傳輸協議:定義了,客戶端和服務器端通信時,發送數據的格式
特點:
1. 基于TCP/IP的高級協議 2. 默認端口號:80 3. 基于請求/響應模型的:一次請求對應一次響應 4. 無狀態的:每次請求之間相互獨立,不能交互數據
1
2
3
4
歷史版本:
1.0:每一次請求響應都會建立新的連接 1.1:復用連接
1
2
請求消息數據格式
請求方式 請求url 請求協議/版本
GET /login.html HTTP/1.1
請求方式:
HTTP協議有7中請求方式,常用的有2種
GET:
1. 請求參數在請求行中,在url后。
2. 請求的url長度有限制的
3. 不太安全
POST:
1. 請求參數在請求體中
2. 請求的url長度沒有限制的
3. 相對安全
請求頭名稱: 請求頭值
常見的請求頭:
User-Agent:瀏覽器告訴服務器,我訪問你使用的瀏覽器版本信息
* 可以在服務器端獲取該頭的信息,解決瀏覽器的兼容性問題
Referer:http://localhost/login.html
告訴服務器,我(當前請求)從哪里來?
作用:
1. 防盜鏈:
2. 統計工作:
空行,就是用于分割POST請求的請求頭,和請求體的。
封裝POST請求消息的請求參數的
> username=zhangsan
1
字符串格式:
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
1
2
3
4
5
6
7
8
9
10
11
Request
request對象和response對象的原理
1. request和response對象是由服務器創建的。我們來使用它們
2. request對象是來獲取請求消息,response對象是來設置響應消息
request對象繼承體系結構:
ServletRequest -- 接口 | 繼承 HttpServletRequest -- 接口 | 實現 org.apache.catalina.connector.RequestFacade 類(tomcat)
1
2
3
4
5
request功能:
1. 獲取請求消息數據
1. 獲取請求行數據
* GET /day14/demo1?name=zhangsan HTTP/1.1
方法:
1. 獲取請求方式 :GET * String getMethod() 2. (*)獲取虛擬目錄:/day14 * String getContextPath() 3. 獲取Servlet路徑: /demo1 * String getServletPath() 4. 獲取get方式請求參數:name=zhangsan * String getQueryString() 5. (*)獲取請求URI:/day14/demo1 * String getRequestURI(): /day14/demo1 * StringBuffer getRequestURL() :http://localhost/day14/demo1 * URL:統一資源定位符 : http://localhost/day14/demo1 中華人民共和國 * URI:統一資源標識符 : /day14/demo1 共和國 6. 獲取協議及版本:HTTP/1.1 * String getProtocol() 7. 獲取客戶機的IP地址: * String getRemoteAddr()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method= req.getMethod(); String contextPath = req.getContextPath(); String servletPath = req.getServletPath(); String queryString = req.getQueryString(); StringBuffer requestURL = req.getRequestURL(); String requestURI = req.getRequestURI(); String protocol =req.getProtocol(); String remoteAddr= req.getRemoteAddr(); System.out.println(method); System.out.println(contextPath); System.out.println(servletPath); System.out.println(queryString); System.out.println(requestURL); System.out.println(requestURI); System.out.println(protocol); System.out.println(remoteAddr); } GET /JavaWeb_tomcat_war_exploded /request1 name=zhangsan http://localhost:8080/JavaWeb_tomcat_war_exploded/request1 /JavaWeb_tomcat_war_exploded/request1 HTTP/1.1 0:0:0:0:0:0:0:1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
獲取請求頭數據
* 方法:
(*)String getHeader(String name):通過請求頭的名稱獲取請求頭的值
Enumeration
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1,獲取請求頭數據 //Enumeration 就可以理解為一個疊加器 Enumeration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String agent = req.getHeader("user-agent"); if(agent.contains("Chrome")){ System.out.println("Chrome-----"); }else if(agent.contains("Firefox")){ System.out.println("Firefox"); } } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String referer= req.getHeader("referer"); if(agent.contains("/JavaWeb_tomcat_war_exploded/request1")){ System.out.println("ok-----"); }else { System.out.println("No-------"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
獲取請求體數據:
請求體:只有POST請求方式,才有請求體,在請求體中封裝了POST請求的請求參數
步驟:
獲取流對象
BufferedReader getReader():獲取字符輸入流,只能操作字符數據
ServletInputStream getInputStream():獲取字節輸入流,可以操作所有類型數據
再從流對象中拿數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
其他功能:
獲取請求參數通用方式:不論get還是post請求方式都可以使用下列方法來獲取請求參數
String getParameter(String name):根據參數名稱獲取參數值username=zs&password=123
String[] getParameterValues(String name):根據參數名稱獲取參數值的數組 hobby=xx&hobby=game
Enumeration
Map
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
中文亂碼問題:
get方式:tomcat 8 已經將get方式亂碼問題解決了
post方式:會亂碼
解決:在獲取參數前,設置request的編碼request.setCharacterEncoding("utf-8");
2. 請求轉發:一種在服務器內部的資源跳轉方式
步驟:
通過request對象獲取請求轉發器對象:RequestDispatcher getRequestDispatcher(String path)
使用RequestDispatcher對象來進行轉發:forward(ServletRequest request, ServletResponse response)
req. getRequestDispatcher(String path).forward(ServletRequest request, ServletResponse response);
1
特點:
瀏覽器地址欄路徑不發生變化
只能轉發到當前服務器內部資源中。
轉發是同一次請求
共享數據:
域對象:一個有作用范圍的對象,可以在范圍內共享數據
request域:代表一次請求的范圍,一般用于請求轉發的多個資源中共享數據
方法:
void setAttribute(String name,Object obj):存儲數據
Object getAttitude(String name):通過鍵獲取值
void removeAttribute(String name):通過鍵移除鍵值對
ServletContext getServletContext()
ServletContext sc=req.getServletContext()
1
HTTP TCP/IP
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。