WEB&&request&&response2022-07-11

?#博學(xué)谷IT學(xué)習(xí)技術(shù)支持#

個(gè)人筆記后續(xù)會(huì)進(jìn)行內(nèi)容改進(jìn)

request:獲取請(qǐng)求數(shù)據(jù)

## 1、獲取請(qǐng)求行的方法:

//? ? ? ? ? ? * 獲取請(qǐng)求方式: `GET`

//? ? ? ? ? ? ? ? ? ? String getMethod()


//? ? ? ? ? ? * 獲取虛擬目錄(項(xiàng)目訪問路徑): `/request-demo`

//? ? ? ? ? ? ? ? ? ? String getContextPath()


//? ? ? ? ? ? * 獲取URL(統(tǒng)一資源定位符): `http://localhost:8080/request-demo/req1`

//? ? ? ? ? ? ? ? ? ? StringBuffer getRequestURL()


//? ? ? ? ? ? * 獲取URI(統(tǒng)一資源標(biāo)識(shí)符): `/request-demo/req1`

//? ? ? ? ? ? ? ? ? ? String getRequestURI()


//? ? ? ? ? ? * 獲取請(qǐng)求參數(shù)(GET方式): `username=zhangsan&password=123`

//? ? ? ? ? ? ? ? ? ? String getQueryString()

String method = req.getMethod();

? ? System.out.println(method);

? ? System.out.println("===============");

? ? String path = req.getContextPath();

? ? System.out.println(path);

? ? System.out.println("===============");

? ? StringBuffer url = req.getRequestURL();

? ? System.out.println(url);

? ? System.out.println("===============");

? ? String requestURI = req.getRequestURI();

? ? System.out.println(requestURI);

? ? System.out.println("===============");

? ? String string = req.getQueryString();

? ? System.out.println(string);

## 2、獲取請(qǐng)求頭:

String getHeader(String name)

## 3、獲取請(qǐng)求體:

對(duì)于請(qǐng)求體中的數(shù)據(jù),Request對(duì)象提供了如下兩種方式來獲取其中的數(shù)據(jù),分別是:

* 獲取字節(jié)輸入流,如果前端發(fā)送的是字節(jié)數(shù)據(jù),比如傳遞的是文件數(shù)據(jù),則使用該方法

* xServletInputStream getInputStream()該方法可以獲取字節(jié)

* 獲取字符輸入流,如果前端發(fā)送的是純文本數(shù)據(jù),則使用該方法

* BufferedReader getReader()

接下來,大家需要思考,要想獲取到請(qǐng)求體的內(nèi)容該如何實(shí)現(xiàn)?

## 遍歷的快捷方式iter!?。。。?!

## 4、request通用方法:

request對(duì)象為我們提供了如下方法:

* 獲取所有參數(shù)Map集合

* Map<String,String[]> getParameterMap()

* 根據(jù)名稱獲取參數(shù)值(數(shù)組)

* String[] getParameterValues(String name)

* 根據(jù)名稱獲取參數(shù)值(單個(gè)值)

* String getParameter(String name)

Map<String, String[]> map = req.getParameterMap();

? ? ? ? for (String k : map.keySet()) {

? ? ? ? ? ? System.out.print(k+":");

String[] vs = map.get(k);

? ? for (String v : vs) {

? ? ? ? System.out.print(v);

? ? }

? ? System.out.println();

}

System.out.println("===========");

String[] hobies = req.getParameterValues("hoby");

for (String hoby : hobies) {

? ? System.out.println(hoby);

}

System.out.println("===========");

System.out.println(req.getParameter("name"));

System.out.println(req.getParameter("pass"));

上面說的是哪個(gè)放在在request和response中通用,不過為了避免代碼重復(fù)

可以在response中用一行代碼解決上述問題

this.doGET() 在response中引用request的方法

this.doGet(req,resp);

## 5、解決中文亂碼問題的代碼(TOMCAT8之后就把get的解碼設(shè)為utf-8)

* GET請(qǐng)求獲取請(qǐng)求參數(shù)的方式是`request.getQueryString()`

* POST請(qǐng)求獲取請(qǐng)求參數(shù)的方式是`request.getReader()`

### 5.1、解決post亂碼:

* 分析出現(xiàn)中文亂碼的原因:

? * POST的請(qǐng)求參數(shù)是通過request的getReader()來獲取流中的數(shù)據(jù)

? * TOMCAT在獲取流的時(shí)候采用的編碼是ISO-8859-1

? * ISO-8859-1編碼是不支持中文的,所以會(huì)出現(xiàn)亂碼

* 解決方案:

? * 頁面設(shè)置的編碼格式為UTF-8

? * 把TOMCAT在獲取流數(shù)據(jù)之前的編碼設(shè)置為UTF-8

? * 通過request.setCharacterEncoding("UTF-8")設(shè)置編碼,UTF-8也可以寫成小寫

**request.setCharacterEncoding("utf-8")是設(shè)置request處理流的編碼**

### 5.2解決get亂碼/post亂碼通用方法:

分析出現(xiàn)中文亂碼的原因:

(1)瀏覽器通過HTTP協(xié)議發(fā)送請(qǐng)求和數(shù)據(jù)給后臺(tái)服務(wù)器(Tomcat)

(2)瀏覽器在發(fā)送HTTP的過程中會(huì)對(duì)中文數(shù)據(jù)進(jìn)行URL==編碼==

(3)在進(jìn)行URL編碼的時(shí)候會(huì)采用頁面`<meta>`標(biāo)簽指定的UTF-8的方式進(jìn)行編碼,`張三`編碼后的結(jié)果為`%E5%BC%A0%E4%B8%89`

(4)后臺(tái)服務(wù)器(Tomcat)接收到`%E5%BC%A0%E4%B8%89`后會(huì)默認(rèn)按照`ISO-8859-1`進(jìn)行URL==解碼==

(5)由于前后編碼與解碼采用的格式不一樣,就會(huì)導(dǎo)致后臺(tái)獲取到的數(shù)據(jù)為亂碼。

解決方案:

name =new String(name.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF-8)

byte[] byte=? ? name.getBytes(StandardCharsets.ISO-8859-1)

name= new String(byte,StandardCharsets.UTF-8)

?6、request轉(zhuǎn)發(fā):forward

#### 6.1、格式:

request.getrequestdispatcher("/demo6").forward(request,response)

dispatcher 調(diào)度轉(zhuǎn)發(fā)

dispatcher

#### 6.2、內(nèi)置三個(gè)方法:

setattribute

getattribute

removeattribute

* 存儲(chǔ)數(shù)據(jù)到request域[范圍,數(shù)據(jù)是存儲(chǔ)在request對(duì)象]中

* void setAttribute(String name,Object o);

* 根據(jù)key獲取值

* Object getAttribute(String name);

* 根據(jù)key刪除該鍵值對(duì)

* void removeAttribute(String name);

#### 6.3、特點(diǎn):

4. 請(qǐng)求轉(zhuǎn)發(fā)的特點(diǎn)

* 瀏覽器地址欄路徑不發(fā)生變化

? 雖然后臺(tái)從`/req5`轉(zhuǎn)發(fā)到`/req6`,但是瀏覽器的地址一直是`/req5`,未發(fā)生變化

? ![1628857365153](F:\java\java SE\線路\八周\Request&Response\講義\assets\1628857365153.png)

* 只能轉(zhuǎn)發(fā)到當(dāng)前服務(wù)器的內(nèi)部資源

? 不能從一個(gè)服務(wù)器通過轉(zhuǎn)發(fā)訪問另一臺(tái)服務(wù)器

* 一次請(qǐng)求,可以在轉(zhuǎn)發(fā)資源間使用request共享數(shù)據(jù)

? 雖然后臺(tái)從`/req5`轉(zhuǎn)發(fā)到`/req6`,但是這個(gè)==只有一次請(qǐng)求==

# response:設(shè)置響應(yīng)數(shù)據(jù)

### yi \重定向格式:兩種方法:

1、

response.setstatus(302)

response.heard("location","/linyi/responsedemo2")

2、簡化版:

response.sendRedirect("/linyi/responsedemo2")

send redirect重定向

**重定向需要添加虛擬路徑**

**轉(zhuǎn)發(fā)不需要:**

**其實(shí)判斷的依據(jù)很簡單,只需要記住下面的規(guī)則即可:**

* **瀏覽器使用:需要加虛擬目錄(項(xiàng)目訪問路徑)**

* **服務(wù)端使用:不需要加虛擬目錄**

![image-20220709191205139](C:\Users\as\AppData\Roaming\Typora\typora-user-images\image-20220709191205139.png)

#### 1、response響應(yīng)字符流

writer=? response.getwriter()

writer.writer("aaaaaaaaa")

#### else:解決response 輸出字符流亂碼

response.setcontentype("text/html;charset=utf-8")? 這行代碼是解決字符流亂碼

#### 2、response響應(yīng)字節(jié)流

編寫一個(gè)讀取字符流

1)可以編寫字節(jié)流copyd代碼,也可以在pom.xml中添加以來坐標(biāo),建議添加

在已有的<dependencies>標(biāo)簽中加入新的<dependency>

<dependency>

? <groupId>commons-io</groupId>

? <artifactId>commons-io</artifactId>

? <version>2.6</version>

</dependency>

2)使用以來中的ioutile。copy方法

IOUtils.copy(inputStream,outputStream);

最后把讀取字符流關(guān)閉即可

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容