首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> ASP 程式設計 >> ASP 基礎

ASP 快速參考指南

     編輯:ASP 基礎

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
ObjectContextCOM+ 交易 (罕用)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 Nexttry-catch、errorPage、Exception 物件
現狀已淘汰 (維護模式)活躍演進 (Jakarta EE 10+)

遷移建議

  1. 評估範圍:頁面數、COM 元件相依、資料庫綁定
  2. 分層重寫:資料存取層 → 服務層 → 控制層 → 視圖層
  3. 資料庫存取:ADO → JDBC / JPA / MyBatis
  4. Session 管理:ASP Session → HttpSession / Spring Session (Redis)
  5. 前端現代化:內嵌 VBScript/JS → 獨立前端框架 + REST API
  6. 部署容器化:IIS → Tomcat/Docker/K8s
  1. 上一篇文章: No
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved