89
Response 物物

Response 物件

Embed Size (px)

DESCRIPTION

Response 物件. 認識 Response 物件. 對 ASP 程式來說 , 就是把資料下載給瀏覽器. Response 物件更進一步的用法. 輸出資料的 Response.Write 方法. 與 HTML 標示混合使用的 Response.Write :簡化版的 Response.Write 將 HTML 標示融入 ASP 程式中. 與 HTML 標示混合使用的 Response.Write. - PowerPoint PPT Presentation

Citation preview

Page 1: Response  物件

Response 物件

Page 2: Response  物件

認識 Response 物件• 對 ASP 程式來說 , 就是把資料下載給瀏覽器

Response.Write "我十八歲,我不抽煙! "

Response 物件

附屬於 Response 物件的方法

輸出到瀏覽器的資料

Page 3: Response  物件

Response 物件更進一步的用法方法或屬性 說明Write 方法 輸出資料到瀏覽器Redirect 方法 改變瀏覽的網頁End 方法 結束資料的輸出Buffer 屬性 是否設定緩衝區Clear 方法 清除緩衝區的資料Flush 方法 輸出緩衝區的資料IsClientConnected 屬性 瀏覽器是否還在連線中

Page 4: Response  物件

輸出資料的 Response.Write 方法• 與 HTML 標示混合使用的 Response.Wri

te• <%= 資料 %> :簡化版的 Response.Write• 將 HTML 標示融入 ASP 程式中

Page 5: Response  物件

與 HTML 標示混合使用的 Response.Write

• 不會變動的資料 , 例如 <BODY BgColor ="#FFFFFF"> 、 <HR> … 等 , 只要原原本本地寫在檔案中就可以;會變動的資料或變數 , 例如 Now 函數 , 才需要使用 Response.Write 來輸出

Page 6: Response  物件

與 HTML 標示混合使用的 Response.Write

Page 7: Response  物件

輸出資料的 Response.Write 方法• 與 HTML 標示混合使用的 Response.Writ

e• <%= 資料 %> :簡化版的 Response.Writ

e• 將 HTML 標示融入 ASP 程式中

Page 8: Response  物件

<%= 資料 %> :簡化版的 Response.Write

• <% 與 %> 是可以跟程式寫在同一行的

Page 9: Response  物件

輸出資料的 Response.Write 方法• 與 HTML 標示混合使用的 Response.Writ

e• <%= 資料 %> :簡化版的 Response.Write• 將 HTML 標示融入 ASP 程式中

Page 10: Response  物件

將 HTML 標示融入 ASP 程式中

Page 11: Response  物件

轉移網頁的 Response.Redirect 方法

Page 12: Response  物件

Redirect 方法的優點• 把較複雜的網頁分成多個子網頁 , 甚至分別放在不同的檔案或目錄 , 然後由主控網頁來判斷當時狀況 , 以決定跳到哪一個子網頁

week = DatePart("w", Date() ) - 1WhichPage = "0" & week & ".htm"Response.Redirect WhichPage

fruit1.asp 00.htm01.htm

06.htm¡K

Page 13: Response  物件

結束資料輸出的 Response.End 方法• Response.End 主要的作用是告訴瀏覽器資料已經全部下載完畢 , 不過它也會促使 I

IS/PWS 不再解讀 ( 譯 ) 後續的 HTML 標示及 ASP 程式 , 而達到結束 .asp 檔案執行的目的

Page 14: Response  物件

結束資料輸出的 Response.End 方法

Page 15: Response  物件

輸出緩衝區的操作• 如果沒有特別設定 , 則呼叫 Response.Write 之後 , PWS/IIS 會直接將我們所要輸出的資料下載給瀏覽器 , 當 Response.Buffer 的屬性值設定為

True, 則會等到以下幾種情況再將緩衝區的資料下載給瀏覽器:– PWS/IIS 解讀 ( 譯 ) 完 ASP 檔案時– 程式呼叫了 Response.End 方法– 程式呼叫了 Response.Flush 方法

Page 16: Response  物件

輸出緩衝區的操作

Page 17: Response  物件

輸出緩衝區的操作• 設定 Response.Buffer 屬性• 送出與取消緩衝區的資料• 輸出緩衝區與筆者的私房菜

Page 18: Response  物件

設定 Response.Buffer 屬性• 必須在沒有任何資料輸出到瀏覽器以前才能將 Response.Buffer 屬性設定為 True, 所以最好將此設定放在 ASP 檔的最前面

Page 19: Response  物件

輸出緩衝區的操作• 設定 Response.Buffer 屬性• 送出與取消緩衝區的資料• 輸出緩衝區與筆者的私房菜

Page 20: Response  物件

送出與取消緩衝區的資料• 可以呼叫 Response.End 或 Response.Fl

ush 強制將緩衝區的資料送出 , 其中 Response.End 會結束程式的執行 , 而 Response.Flush 則會繼續執行程式 , 此外 , 也可以呼叫 Response.Clear 中途取消 ( 清除 ) 緩衝區的資料

Page 21: Response  物件

送出與取消緩衝區的資料

Page 22: Response  物件

輸出緩衝區的操作• 設定 Response.Buffer 屬性• 送出與取消緩衝區的資料• 輸出緩衝區與筆者的私房菜

Page 23: Response  物件

輸出緩衝區與筆者的私房菜• PWS 並沒有提供讓我們一邊執行一邊檢查程式執行的測試環境 , 建議可以使用

Response.Write 來檢視資料的內容• 利用 Response.Buffer 將 Output 敘述把資料寫到緩衝區 , 若程式執行正常 , 會執行 Response.Clear 清除緩衝區;若執行錯誤 , 則會下載錯誤訊息 , 顯示在網頁上 , 供我們判斷

Page 24: Response  物件

輸出緩衝區與筆者的私房菜

Page 25: Response  物件

檢查瀏覽器是否還在連線中• 當我們撰寫比較複雜的 ASP 程式時 , 應該檢查瀏覽器是否還在連線中• 須判斷 Response.IsClientConnected, 若此屬性值為 True, 表示瀏覽器還在連線中 , 若為 False, 則表示已經離線

Page 26: Response  物件

Request 物件與輸入表單

Page 27: Response  物件

認識 Request 物件• Request 物件是用來讀取瀏覽器的資料 , 當我們在網址輸入:

過程如下:瀏覽器 IIS/PWS

Name =KJWang& Love =VB

Request物件

Echo.asp

1.解析

Request(" Name ") ="KJWang"Request(" Love ")= "VB"

2.啟動

3.讀取Name =KJWang& Love =VB

參數分隔符號

參數 參數值

Page 28: Response  物件

讀取 HTML 的表單欄位• Request 物件除了可以用來讀取附帶在網址後面的參數之外 , 也可以讀取 HTML 表單欄位的內容

Page 29: Response  物件

表單的組成要件• 典型的 HTML 表單包含幾個要件:

– 輸入欄位– 傳送鈕– 按下傳送鈕之後 , Server 端所啟動之 CGI 或

ASP 程式

Page 30: Response  物件

表單的組成要件• 本例中 , 其他設定:

– 按下傳送鈕所啟動的 ASP 程式:設定為 Echo.asp

– 輸入欄位名稱:分別設定為 Name 及 Love

輸入欄位

傳送鈕

按下後啟動 Echo.asp

Page 31: Response  物件

HTML 表單設計的基礎

<FORM> ¤Î </FORM> 裡面的輸入欄位都屬於這個表單

每個欄位都會對應到表單裡面的一個標示

Page 32: Response  物件

同一表單的輸入資料欄位• 一個 .htm 檔案可以同時存在多個輸入表單 , 但只有同一表單的輸入資料才會一起傳送到 Server, 因此要同時傳送到 Serve

r 的資料 , 其輸入欄位應佈置在同一表單中

Page 33: Response  物件

設定表單的屬性• 表單的 Action 屬性 , 其作用為指定 Serve

r 端處理此一表單的程式• 表單的 Method 屬性 , 指的是表單資料傳送到 Server 的方式 , 設定值有 2 種

傳送方法 傳送方式及效能 傳送之資料量GET 立刻傳送 , 執行效能高 可傳送的資料只有 2K 左右POST 等待 Server 來讀取資料 可傳送較大量的資料

Page 34: Response  物件

設定表單的屬性• 文字輸入框的 Name ( 名稱 ) 屬性 , 利用 Request(" Name ") 及 Request(" Lo

ve") 來讀取這兩個文字輸入框的資料• 按鈕的 Type ( 按鈕類型 ) 屬性 , 其預設為傳送 (Submit), 表示按下後瀏覽器會傳送資料到 Server 端。另外還可設定為重置 (Reset) 及一般 (Button)。「重置」是清除表單上的資料 , 讓上網者重新輸入 , 「一般」則是用來啟動瀏覽器的

Script 程式

Page 35: Response  物件

文字輸入欄位• 在 HTML 表單中 , 有 3 種文字輸入欄位可運用:

輸入欄位 說明 使用例文字輸入框 一般的文字輸入框 , 只能輸入單行文字密碼輸入框 與文字輸入框類似 , 但輸入的字元會顯示成 *文字輸入區 可以輸入多行文字的文字輸入框

Page 36: Response  物件

使用文字輸入區的注意事項• 最好將表單的 Method 屬性設定成 POST, 若設定成 GET, 則受限於可傳輸的資料量 , 可能造成文字被截斷的現象• 若想在 ASP 程式中將文字輸入區的資料顯示出來 , 須注意跳行問題

Page 37: Response  物件

實例 (訪客留言板 )

輸入框靠左對齊

「留言:」與文字輸入區靠上對齊

Page 38: Response  物件

實例 (訪客留言板 )

• 要讓兩個文字輸入框對齊 , 須藉助表格

Page 39: Response  物件

實例 (訪客留言板 ) 的設計重點1. 表格在表單內 2. 表格框線的寬度為 03. 設定對齊方式

Page 40: Response  物件

下拉式選單• 下拉式選單是先把所有的資料列出來 , 供使用者選取

Page 41: Response  物件

核取方塊• 核取方塊與「允許多重選擇的下拉式選單」很類似 , 不同的是 , 核取方塊的每一個選項則是個別獨立的輸入欄位

( )允許多重選擇的下拉式選單 ( )核取方塊

四個輸入欄位一個輸入欄位

Page 42: Response  物件

選擇鈕• 選擇鈕是提供單選的功能• 同一組選擇鈕應該設定成相同的名稱以及不同的值

Page 43: Response  物件

選擇鈕的特性與設定

Page 44: Response  物件

其他類型的表單欄位• 隱藏 (Hidden ) 欄位

在表單上看不到 , 但資料卻會傳到 Server 的欄位

Page 45: Response  物件

其他類型的表單欄位• 重置 (Reset) 按鈕

其作用是將表單上所有已輸入的資料還原成預設值

輸入資料

-(1)圖 -(2)圖

Page 46: Response  物件

ASP 物件與網頁製作應用

Page 47: Response  物件

Application 物件• 利用 Application 物件來記錄變數內容• 訪客計數器• 圖形的訪客計數器

Page 48: Response  物件

Application 物件• 在網頁結束執行時 , 還能夠保留變數內容 , 以備下次使用 , 可藉助 Application 物件

Page 49: Response  物件

Application 物件• 利用 Application 物件來記錄變數內容• 訪客計數器• 圖形的訪客計數器

Page 50: Response  物件

訪客計數器

Page 51: Response  物件

訪客計數器非同步更新的解決方法1. 讀取 Application ("counter") 的值2. 將讀取的值加一3. 將加一之後的值指定回 Application ("cou

nter")

Page 52: Response  物件

Lock / Unlock

• 當兩位連線者同時執行時 , 會少加 1次 , 為避免此狀況:

Page 53: Response  物件

Application 物件的生命週期

網頁

… 其他網頁

網頁

IIS/PWS Application 物件生於此

Application 物件滅於此

變數生於此

變數滅於此變數生於此

變數滅於此

變數的生命週期

變數的生命週期

Application 物件的生命週期

Page 54: Response  物件

保證訪客計數器的數字不會流失• 為避免訪客計數器不會流失 , 將訪客計數器寫到檔案中是最普遍的作法。

Page 55: Response  物件

保證訪客計數器的數字不會流失• <!--#include virtual=" /kjasp/func /FileCnt.f

un" -->將 FileCnt.fun 整個加到此 ASP 檔案中• kjFileCounter(" /counter.txt ")呼叫 kjFileCounter 函數並傳入 /counter.t

xt 檔案為參數

Page 56: Response  物件

Application 物件• 利用 Application 物件來記錄變數內容• 訪客計數器• 圖形的訪客計數器

Page 57: Response  物件

圖形的訪客計數器

Page 58: Response  物件

圖形的訪客計數器• 以上所製作的訪客計數器是以文字來輸出 , 如果我們想顯示圖形的訪客計數器 , 例如 , 那麼並不需要修改 kjFileCounter 函數 , 只要把計數器的數值轉換成圖形計數器的 HT

ML 標示即可

Page 59: Response  物件

圖形的訪客計數器

Page 60: Response  物件

Session 物件• 可在網頁結束時將變數的內容儲存下來• Session 物件的使用必須配合瀏覽器的 Co

okies 功能方才有效 , 因此在您測試以上程式時 , 請先檢查瀏覽器的 Cookies 功能是否為開啟的

Page 61: Response  物件

Session 物件• Session 物件與 Application 物件的差異• Session 物件的實際應用• 使用 Session 物件不可不知的問題

Page 62: Response  物件

Session 物件與 Application 物件的差異• Application 是所有連線所有網頁共用的物件 , 而 Session 是某一連線所有網頁的共用物件

(a) 瀏覽器與 Session 物件的關係 (b) 瀏覽器與 Application 物件的關係

瀏覽器

瀏覽器

瀏覽器

¡K

Application物件

ASP

瀏覽器

瀏覽器

瀏覽器

¡K

Session 物件

Session 物件

Session 物件

ASP

Page 63: Response  物件

Session 物件• Session 物件與 Application 物件的差異• Session 物件的實際應用• 使用 Session 物件不可不知的問題

Page 64: Response  物件

Session 物件的實際應用一• 必須輸入帳號及密碼的網頁

表單一

表單二

表單三

(啟動 Query1.asp)

(啟動 Query2.asp)

(啟動 Query3.asp)

Page 65: Response  物件

Session 物件的實際應用一• 為避免讓使用者重複輸入帳號及密碼

:帳號 kjwang

:密碼 ******

登入之網頁 (Login.htm) 檢查密碼之網頁 (Check.asp)

1.查詢本月使用時數2.查詢餘額3.線上轉帳

:請選擇功能

QueryC.asp

QueryB.asp

QueryA.asp

Page 66: Response  物件

Session 物件的實際應用二• 強迫進入首頁的網頁

Page 67: Response  物件

Session 物件的實際應用三• 幸運號碼贈獎網頁

Page 68: Response  物件

Session 物件• Session 物件與 Application 物件的差異• Session 物件的實際應用• 使用 Session 物件不可不知的問題

Page 69: Response  物件

使用 Session 物件不可不知的問題• 上網者關閉了 Cookies 功能

Page 70: Response  物件

使用 Session 物件不可不知的問題• 斷了線的 Session 物件

–延長 IIS/PWS 斷線的時間– 使用 Cookies 物件

Page 71: Response  物件

Cookies 物件• Application 物件及 Session 物件是把資訊記錄在 Server 這一端 , 而 Cookies 物件是將資訊記錄在客戶端• Cookies 物件跟 Session 物件一樣 , 必須配合瀏覽器的 Cookies 功能方才有效• Cookies 物件是附屬於 Response 及 Req

uest 物件的屬性 , 使用時須在前面加上 Response 及 Request

Page 72: Response  物件

Cookie 物件• 從 Application 物件到 Cookie 物件• Cookies 的生命週期

Page 73: Response  物件

從 Application 物件到 Cookie 物件

Page 74: Response  物件

注意 Request.Cookies 的傳回值• 在沒有資料的情況下 , Application 物件及

Session 物件的傳回值是 Empty , 而 Request.Cookies 卻是傳回 "" (空字串 )

Page 75: Response  物件

Cookies 是記錄在瀏覽器的資訊瀏覽器 Server

啟動網頁

(網頁表頭資訊 包含 Cookies )的交換

瀏覽器與 Server 之間的資料傳遞: 階段一

瀏覽器 Server

下載資料

瀏覽器與 Server 之間的資料傳遞: 階段二

Page 76: Response  物件

幾個容易弄錯的敘述

Cookies瀏覽器

讀取

寫入

Request

ResponseIIS/PWS

Page 77: Response  物件

總整理• 讀取

物件 敘述Application data1 = Application("data1")Session data1 = Session("data1")

Request data1 = Request("data1")Response XCookies data1 =

Request.Cookies("data1")

Page 78: Response  物件

總整理• 寫入

物件 敘述Application Application("data1") = data1

Session Session("data1") = data1

Request X

Response Response.Write data1

Cookies Response.Cookies("data1") = data1

Page 79: Response  物件

Cookie 物件• 從 Application 物件到 Cookie 物件• Cookies 的生命週期

Page 80: Response  物件

Cookies 的生命週期資料種類 生 滅變數 網頁被瀏覽時 網頁被解讀 ( 譯 ) 完時Session 瀏覽器第一次與 Serve

r 連線時1. 瀏覽器結束執行時2. 瀏覽器超過一段時間沒有向

IIS/PWS 索求任何網頁Application IIS/PWS 開始執行時 1. IIS/PWS 結束執行時

2. 沒有任何瀏覽器與 IIS/PWS 連線一段時間以後Cookies 瀏覽器被執行時 瀏覽器結束執行時

Page 81: Response  物件

Cookies 的生命週期是可以設定的• 想要在瀏覽器結束執行之後 , 將某一份儲存於 Cookies 之中的資料保存下來 , 可在執行「 Response.Cookies(" 資料 ") = 資料」之後 , 設定其 Expires 屬性

Page 82: Response  物件

設定使用期限的 Cookies

沒有設定使用期限的 Cookies

沒有設定使用期限的 Cookies

沒有設定使用期限的 Cookies

瀏覽器

瀏覽器

瀏覽器

¡K

設定使用期限的 Cookies

共用

各自擁有用

各自擁有用

各自擁有用

Page 83: Response  物件

不同瀏覽器的 Cookies 並不相容IE

瀏覽器

IE 建立之Cookies

NC瀏覽器

NC 建立之Cookies

Page 84: Response  物件

Server 物件及 Server 相關資訊• 相對路徑、虛擬路徑、與實際路徑• Request.ServerVariables 方法• Server 物件

Page 85: Response  物件

相對路徑、虛擬路徑、與實際路徑• 只有檔名 , 例如 , "stone.jpg" 、 "vbapi.ht

m" 、 "counter.txt"。• 相對路徑 , 例如 , "picture/stone.jpg" 、 "a

pi/vbapi.htm" 、 "../vbapi.htm"。• 絕對路徑 , 例如 , "/counter.txt" 、 "/Count

er/counter.txt"。

Page 86: Response  物件

Request.ServerVariables 方法• 利用 ServerVariables 方法讀取 Server 所提供的資訊

– 讀取 IP 位址– 讀取目錄檔案資訊– 讀取附帶在網址後面的參數

Page 87: Response  物件

Server 物件• Server.MapPath :網址路徑與實際路徑的對照

呼叫的敘述 傳回值Server.MapPath("/") C:\Inetpub\wwwroot

Server.MapPath("/abc.txt") C:\Inetpub\wwwroot\abc.txt

Server.MapPath("/ch07/9.gif") C:\Inetpub\wwwroot\kjasp\ch07\9.gif

Page 88: Response  物件

Server 物件• Server.HTMLEncode : HTML 編碼

<%=Server.HTMLEncode("<B>這是不是粗體字 </B>")%>

<B>這是不是粗體字 </B><%="<B>這是不是粗體字 </B>"%>

Page 89: Response  物件

Server 物件• Server.CreateObject :建立 ActiveX 物件