ASP (Active Server Pages) 快速參考指南
ASP 是 Microsoft 早期伺服器端腳本技術 (1996),運行於 IIS,主要使用 VBScript/JScript。已由 ASP.NET Core 取代,本文僅供維護舊系統參考。
基本語法
<%@ Language="VBScript" %>
<html><body>
<% Response.Write("Hello World!") %>
</body></html>
- 腳本區塊:
<% ... %>
- 輸出簡寫:
<%= 運算式 %> (等同 Response.Write)
- 註解:
' 這是 VBScript 註解
- 指令:
<%@ Language="VBScript" %>、<%@ CodePage=65001 %> (UTF-8)
內建物件
| 物件 | 用途 | 常用成員 |
Request | 客戶端請求資訊 | QueryString, Form, Cookies, ServerVariables, BinaryRead |
Response | 伺服器回應控制 | Write, Redirect, Cookies, Buffer, CacheControl, ContentType, Expires, AddHeader, End, Flush |
Session | 用戶會話狀態 | Contents, StaticObjects, SessionID, Timeout, Abandon, CodePage, LCID |
Application | 全域應用狀態 | Contents, StaticObjects, Lock, Unlock |
Server | 伺服器工具 | CreateObject, Execute, Transfer, MapPath, HTMLEncode, URLEncode, GetLastError, ScriptTimeout |
ObjectContext | COM+ 交易 (罕用) | SetComplete, SetAbort |
表單處理
<!-- GET 表單 -->
<form action="process.asp" method="get">
姓名:<input name="name"> <input type="submit">
</form>
<!-- process.asp --%>
<% name = Request.QueryString("name") %>
<!-- POST 表單 -->
<form action="process.asp" method="post">
姓名:<input name="name"> <input type="submit">
</form>
<!-- process.asp --%>
<% name = Request.Form("name") %>
Cookie 操作
<%
' 寫入 (必須在 之前)
Response.Cookies("username") = "admin"
Response.Cookies("username").Expires = DateAdd("d", 30, Now())
Response.Cookies("username").Path = "/"
' 讀取
username = Request.Cookies("username")
' 刪除
Response.Cookies("username").Expires = DateAdd("d", -1, Now())
%>
檔案包含
<!-- 虛擬路徑 (相對網站根目錄) -->
<!-- #include virtual="/include/header.asp" -->
<!-- 相對路徑 (相對當前檔案) -->
<!-- #include file="../include/header.asp" -->
Global.asa (應用/會話事件)
<script language="vbscript" runat="server">
Sub Application_OnStart
Application("onlineCount") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application("onlineCount") = Application("onlineCount") + 1
Application.Unlock
Session("loginTime") = Now()
End Sub
Sub Session_OnEnd
Application.Lock
Application("onlineCount") = Application("onlineCount") - 1
Application.Unlock
End Sub
</script>
<object runat="server" scope="application" id="MyComponent" progid="MyCom.Component" />
常用元件 (Server.CreateObject)
Scripting.FileSystemObject:檔案系統操作
ADODB.Connection / Recordset:資料庫存取
CDO.Message:發送郵件
MSXML2.XMLHTTP:HTTP 請求
Scripting.Dictionary:雜湊表
錯誤處理
<% On Error Resume Next %>
<%
' 有風險的操作
Conn.Open connStr
If Err.Number <> 0 Then
Response.Write "錯誤:" & Err.Description
Err.Clear
End If
%>
<!-- 自訂錯誤頁 (IIS 設定) -->
ASP vs JSP 快速對照
| 特性 | ASP (Classic) | JSP / Jakarta Server Pages |
| 執行環境 | IIS (Windows) | 任何 Servlet 容器 (跨平台) |
| 語言 | VBScript / JScript (解釋執行) | Java (編譯為 Servlet 類別) |
| 效能 | 較低 (每請求解釋) | 高 (編譯後類別常駐、多執行緒) |
| 物件模型 | COM 元件 (CreateObject) | JavaBean / EJB / Spring Bean |
| 標籤庫 | 無 (僅 #include) | JSTL、自訂標籤、Tag Files |
| 錯誤處理 | On Error Resume Next | try-catch、errorPage、Exception 物件 |
| 現狀 | 已淘汰 (維護模式) | 活躍演進 (Jakarta EE 10+) |
遷移建議
- 評估範圍:頁面數、COM 元件相依、資料庫綁定
- 分層重寫:資料存取層 → 服務層 → 控制層 → 視圖層
- 資料庫存取:ADO → JDBC / JPA / MyBatis
- Session 管理:ASP Session → HttpSession / Spring Session (Redis)
- 前端現代化:內嵌 VBScript/JS → 獨立前端框架 + REST API
- 部署容器化:IIS → Tomcat/Docker/K8s