Servlet的Responset物件原理和使用

2021-10-10 18:28:12 字數 2235 閱讀 4090

2、servletcontext物件:

格式:http/1.1 200 ok

設定狀態碼:setstatus(int sc)

setheader(string name, string value)

獲取輸出流

使用輸出流,將資料輸出到客戶端瀏覽器

1. 完成重定向

//1. 設定狀態碼為302

response.

setstatus

(302);

//2.設定響應頭location

response.

setheader

("location"

,"/day15/responsedemo2");

//簡單的重定向方法

response.

sendredirect

("/day15/responsedemo2"

);

2. forward 和 redirect 區別

路徑分類

1. 相對路徑:通過相對路徑不可以確定唯一資源

如:./index.html

不以/開頭,以.開頭路徑

2.規則:找到當前資源和目標資源之間的相對位置關係

./:當前目錄

…/:後退一級目錄

絕對路徑:通過絕對路徑可以確定唯一資源

如:http://localhost/day15/responsedemo2 /day15/responsedemo2

以/開頭的路徑

3. 伺服器輸出字元資料到瀏覽器

//簡單的形式,設定編碼,是在獲取流之前設定

response.

setcontenttype

("text/html;charset=utf-8"

);

4. 伺服器輸出位元組資料到瀏覽器

5. 驗證碼

本質:目的:防止惡意表單註冊

概念:代表整個web應用,可以和程式的容器(伺服器)來通訊

獲取:1. 通過request物件獲取

request.getservletcontext();

2. 通過httpservlet獲取

this.getservletcontext();

功能:

獲取mime型別:

mime型別:在網際網路通訊過程中定義的一種檔案資料型別

格式: 大型別/小型別 text/html image/jpeg

獲取:string getmimetype(string file)

域物件:共享資料

1. setattribute(string name,object value)

2. getattribute(string name)

3. removeattribute(string name)

獲取檔案的真實(伺服器)路徑

1. 方法:string getrealpath(string path)

string b = context.

getrealpath

("/b.txt");

//web目錄下資源訪問

system.out.

println

(b);

string c = context.

getrealpath

("/web-inf/c.txt");

//web-inf目錄下的資源訪問

system.out.

println

(c);

string a = context.

getrealpath

("/web-inf/classes/a.txt");

//src目錄下的資源訪問

system.out.

println

(a);

servletcontext物件範圍:所有使用者所有請求的資料

1. 獲取檔名稱

2. 使用位元組輸入流載入檔案進記憶體

3. 指定response的響應頭: content-disposition:attachment;filename=***

4. 將資料寫出到response輸出流

問題:

中文檔案問題

解決思路:

1. 獲取客戶端使用的瀏覽器版本資訊

2. 根據不同的版本資訊,設定filename的編碼方式不同

Burpsuite設定攔截response

一 burpsuite設定攔截http https 1,攔截修改request 首先進入proxy options intercept client requests設定request攔截的規則 如果不勾選intercept requests based on the following rules...

Respons功能介紹 重定向以及特點

respons物件 功能 設定響應訊息 1.設定響應行 1.格式 http 1.1 200 ok 2.設定狀態碼 setstatus int sc 2.設定響應頭 setheader string name,string value 3.設定響應體 使用步驟 1.獲取輸出流 符輸出流 只能輸出字元資...

Servlet學習 servlet的生命週期

servlet介面中定義了作為乙個servlet在整個生命週期中應該擁有三個階段 1,初始化 2,服務 3,銷毀 servlet的生命週期是由容器管理的 servlet的生命週期 簡化版 適用於筆試的時候 1,servlet的生命週期是由容器管理的 這句話非常重要 2.他分別經歷三個階段 初始化 服...