1.03k likes | 1.26k Views
ASP. 資策會教育訓練處 楊宏文 hwyang@iii.org.tw. 何謂 ASP. 全名 Active Server Pages 。 利用 Script 語法,學習無障礙。 可利用 Inter Dev 自動產生程式碼。 語法的大小寫皆可 。 任何瀏覽器均支援。 任何資料庫均支援。. ASP 的使用環境. W indows 9x : PWS 4.0 Windows NT Workstation : PWS 4.0 Windows NT Server : IIS 4.0
E N D
ASP 資策會教育訓練處 楊宏文 hwyang@iii.org.tw
何謂ASP • 全名Active Server Pages。 • 利用Script 語法,學習無障礙。 • 可利用 Inter Dev 自動產生程式碼。 • 語法的大小寫皆可。 • 任何瀏覽器均支援。 • 任何資料庫均支援。
ASP的使用環境 Windows 9x :PWS 4.0 Windows NT Workstation:PWS 4.0 Windows NT Server:IIS 4.0 Windows 2000 Server/Professional:IIS 5.0 Windows XP Professional:IIS 5.1 Windows 2003:IIS 6.0
軟體來源 – Personal Web Server Windows 9x :PWS 4.0 1.原版光碟中add-ons\pws目錄或NT4.0 Option Pack 中 Win.95目錄。 2.執行 setup.exe 進行安裝。 Windows NT Workstation 4.0:PWS 4.0 1.NT4.0 Option Pack 中 Winnt.wks目錄。 2.執行 setup.exe 進行安裝。
軟體來源 – Internet Information Server Windows NT Server 4.0:IIS 4.0 1.Option Pack 中 Winnt.srv目錄。 2.執行 setup.exe 進行安裝。 Windows 2000 Server/Professional:IIS 5.0 內建即包含 IIS 5.0 Windows XP Professional:IIS 5.1 (內建) Windows 2003:IIS 6.0(內建)
安裝注意事項 – PWS on Win95 升級 Winsock 到 2.0。 安裝程式與 PWS 在同一目錄下,名為 Ws2setup.exe。
安裝注意事項 – PWS/IIS on NT 4.0 1.升級 NT 到 Service Pack 3 以上版本。 Service Pack 3,檔名為Nt4sp3_i.exe。 Service Pack 4,檔名為sp4i386.exe。 Service Pack 5,檔名為sp5i386.exe。 Service Pack 6,檔名為sp6i386.exe。 2.升級 IE 到 4.01 以上版本。
作業平台安裝步驟 • Win95:IE5.0 Winsock 2.0 PWS 4.0。 • Win98:PWS 4.0。 • Win NT W/S:SP3 IE 5.0 PWS 4.0。 • Win NT Server:SP3 IE 5.0 IIS 4.0。 • Windows 2000:IIS 5.0 • Windows XP:IIS 5.1 • Windows 2003:IIS 6.0
作業平台運作之檢查 -PWS 1.啟動 PWS: 開始 程式集 Internet Explorer Personal Web Server 個人 Web 管理員 2.打開瀏覽器,於網址列輸入IP位址,檢查 是否安裝妥當。
作業平台運作之檢查 -IIS 1.啟動 IIS: 開始 程式集 系統管理工具 Internet Information Server Internet 服務管理員 或 開始 執行 輸入inetmgr 2.打開瀏覽器,利用IP檢查是否安裝妥當。
下載 HTML文件, 由客戶端瀏覽器執行 *.htm *.htm Web Server vs. Browser Web Server Client 端
下載 HTML文件, 由客戶端瀏覽器執行 *.asp *.htm Web Server vs. Browser Web Server 執行 asp 檔案,將結果轉成 HTML 格式 Web Server Client 端
<錯誤> <正確> 撰寫ASP之前… • 所有 ASP 的檔案副檔名應為 asp。 • ASP的程式碼應該存放在 <%…………%> • 標籤之內。 • ASP 檔案的執行,應該透過 asp-enable • 的 web 伺服器編譯,方能執行。
撰寫ASP之前… • 語法使用大小寫皆可。 • 伺服器將主動提供除錯的訊息。 • 敘述句結尾不使用分號。 • 通常搭配 VBScript 的語法,如欲使用JavaScript語法,需先使用以下指令: • <% @language=“JavaScript” %>
ASP的六大物件 • Response:將資料送給客戶端。 • Request:從客戶端取得資料。 • Server:提供伺服器運作參數。 • Session:儲存在一個Session內的使用者資訊,該資訊僅可為該使用者取用。 • Application:儲存在一個Application內的使用者資訊,該資訊可為所有使用者取用。 • ObjectContext:用來配合Microsoft Transction Server進行分散式交易處理。
ASP Response 物件
回應物件(Response Object) • 用以將資訊傳送至客戶端。 • 常見的應用如下: • Response.Write • Response.Cookies
Response.Write • 使用格式有以下幾種類型: <% Response .Write (“字串”) %> <% Response .Write “字串” %> <% Response .Write變數名稱 %> <% = 變數名稱 %> <% Response .Write (“字串”) &變數名稱%> <% Response .Write “字串” & 變數名稱%> <% Response .Write (“字串” & 變數名稱)%>
別忘了! Asp檔案必須透過Web 伺服器的解譯 方能執行 Response.Write 範例一 <% Response.write("<FONT COLOR=red>") Response.write("我的第一個 Active Server Page 程式") Response.write("</FONT><BR>") Response.write "今天日期是: " & date & "<BR>" Response.write ("現在時間是: ")& now %>
Response.Write 範例二 <FONT COLOR=red> 我的第一個 Active Server Page 程式 </FONT><BR> 今天日期是:<% = date %><br> 現在時間是:<% = now %> <% Response.write xxx %> 如果 xxx 是變數、常數、或運算式,可以簡寫為 <% =xxx %>
ASP 註解與變數
使用註解 • 藉以提高程式之可讀性。 • 使用單引號(’)或 Rem標註。 ‘這是一行註解 Rem這也是一行註解
使用變數 • 使用 Dim標註。 • 以屬性值決定變數之資料型態。 • 可以省略定義的動作。 Dim subject subject = “Hubert”
加入變數及註解 <% '定義一個變數 dim name name = "Hubert" '傳送訊息字串 response.write ("<li>我是" & name) response.write ("<li>我是<b>" & name & "</b>") %>
ASP 迴圈
For 迴圈 <% For 變數=初值 To終值 Step變動量 程式碼 Next %> • 若變動量為 1,可省略Step子句。
For 迴圈的使用 <% '將訊息字串以 write 方法傳送 for i=0 to 2 response.write ("Hubert is handsome.<br>") next %>
For 迴圈的使用 <% '夾雜非 VBScript 程式碼 for i=0 to 2 %> Hubert is handsome.<br> <% next %>
For 迴圈的使用 <% '將訊息字串以 write 方法傳送 for i=1 to 6 response.write ("<font size=" & i & _ ">Hubert is handsome.<br></font>") next %>
For 迴圈的使用 <% '夾雜非 VBScript 程式碼 for i=1 to 6 %> <font size=" <%=i%> "> Hubert is handsome.</font><br> <% next %>
Do...Loop 迴圈 • Do 迴圈的語法頗多,其中最常見的是Do While...Loop 。 <% Do While 條件式 程式碼 Loop %>
Do...Loop 迴圈執行流程 進入迴圈 條件式 True False 程式敘述 離開迴圈
Do...Loop 迴圈的使用 <% i=1 sum=0 do while i<=10 sum = sum + i i=i+1 loop response.write("1+2+...+10 = " & sum) %>
IF 判斷敘述 • 單一敘述式。 <% If 條件式 Then 程式碼 End if %>
IF 判斷敘述 • 二選一敘述式。 <% If 條件式 Then 程式碼 Else 程式碼 End if %>
IF 判斷敘述 • 多選一敘述式。 <% If 條件式 Then 程式碼 Elseif 條件式 Then 程式碼 Else 程式碼 End if %>
IF 敘述句的使用 <% dim handsome handsome = true '請注意條件式的寫法 if (handsome=true) then response.write ("的確是不錯") else response.write ("其實也還好") end if %>
何謂 Cookie • Cookies 是用以儲存在客戶端磁碟裡的參考資訊。 • 如果關鍵字名稱已經存在,新的設定值將取代舊的。 KEY = VALUE ex. Name = hubert
Cookie 的儲存方式 • 以檔案的方式儲存於系統夾之 Profiles下的Cookies資料夾內。 • ex.C:\winnt\profiles\administrator\Cookies\ • C:\Documents and settings\administrator\Cookies\ administrator@hubert.idv.tw.txt
定義Cookies 的方式 1.利用Response.Cookies定義關鍵字值。 2.利用 Expires 參數指定該Cookies的有效 時間。 Response.Cookies("關鍵字") = "屬性值" Response.Cookies("關鍵字").Expires = "JAN 30, 2004"
讀出 Cookies 的方式 利用Request.Cookies指明欲讀取的關鍵 字值。 Request.Cookies("關鍵字")
Cookies 範例一 <% Response.Cookies("name")= " hubert" Response.Cookies("name").Expires=Date+365 %> 執行結果:
Cookies 範例二 – Cookies.asp <% '將資訊寫入 Cookies 當中 Response.Cookies("name")="楊宏文" Response.Cookies("name").Expires=Date+365 '從 Cookies 當中將資訊拿出來 UserName=Request.Cookies("name") %> <h1><%=userName%></h1>
使用 Cookies 的注意事項 Response.Cookies 的程式碼必須位於整 份 html 文件的最前面,否則會有如下 的錯誤。
Cookies 範例三 – getCookies.asp <% if Request("Name")<>"" then Response.Cookies("name")=Request("Name") Response.Cookies("name").Expires=Date+365 end if response.write request.Cookies("name") %> <form> <input type=text name=Name> <input type=submit> </form>
ASP Request 物件
請求物件(Request Object) • 用以接收來自客戶端的訊息。 REQUEST[.資料集合](變數名稱)
Request 的應用 取得使用者輸入。 ex.留言版、網路購物、意見調查 線上投票……。
練習 1-1 1.構成如下圖之歡迎畫面。 2.將使用者名稱寫入 Cookies 。 ex1-1-welcome.htm ex1-1-getCookies.asp
簡易留言版的設計 1.先用 HTML 建構表單及外觀。 guest.htm 2.撰寫 ASP 解析並處理使用者傳送之資 料值。 gBook.asp