ASP Server

Server 的 CreateObject / MapPath / HTMLEncode / URLEncode / Execute / Transfer。

Intermediate
Key PointsServer 提供「造对象、转路径、转义、包含执行」四类基础能力,是 ASP 里最常用的工具对象。

成员一览

MemberPurpose
CreateObjectADODB.Connection、Scripting.FileSystemObject 等
MapPath/data/a.mdb → C:\inetpub\...\data\a.mdb
HTMLEncode< → &lt;,防 XSS
URLEncode中文与特殊字符编码
Execute在当前页执行另一个 .asp
Transfer转到另一个页,保留 request 上下文
ScriptTimeout最长执行秒数
GetLastError自定义错误处理时读错误对象

CreateObject

ASP
<%
    Dim fso, conn, dict
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set conn = Server.CreateObject("ADODB.Connection")
    Set dict = Server.CreateObject("Scripting.Dictionary")

    ' 用完置 Nothing,让 COM 对象尽早释放
    Set dict = Nothing
    Set conn = Nothing
    Set fso = Nothing
%>

Set is required for object assignment; fso = ... raises "object variable not set". Assign Nothing when done — the page end would release it anyway, but large objects benefit from early release.

MapPath

ASP
<%
    Response.Write Server.MapPath("/") & "<br>"
    Response.Write Server.MapPath("/data/app.mdb") & "<br>"
    Response.Write Server.MapPath("list.asp")
%>
Output
C:\inetpub\wwwroot\
C:\inetpub\wwwroot\data\app.mdb
C:\inetpub\wwwroot\products\list.asp

MapPath converts a web path to a physical path, which all file operations need. A leading / means the site root; otherwise it is relative to the current page's directory.

HTMLEncode 与 URLEncode

ASP
<%
    Dim raw
    raw = "<script>alert(1)</script>"

    Response.Write Server.HTMLEncode(raw) & "<br>"
    Response.Write Server.URLEncode("中文 参数&x=1")
%>
Output
&lt;script&gt;alert(1)&lt;/script&gt;
%D6%D0%CE%C4+%B2%CE%CA%FD%26x%3D1

HTMLEncode is the basic XSS defence: <, >, & and " become entities and the browser shows them as text. URLEncode is for building URLs with Chinese parameters.

HTMLEncode only protects HTML context. Inside <script>, an onclick attribute or href="javascript:..." it does nothing; those contexts need entirely different handling.

Execute 与 Transfer

ASP
<%
    ' Execute:在当前页执行另一个页,共享变量
    Server.Execute "part.asp"

    ' Transfer:转到另一个页,后续代码不执行,URL 不变
    Server.Transfer "done.asp"
%>

Execute inlines the other page and returns; Transfer hands over and never comes back. Neither changes the address bar, unlike Response.Redirect.

超时控制

ASP
<%
    Server.ScriptTimeout = 300        ' 最多执行 300 秒
%>

The default is 90 seconds. Long exports and imports need more, but do not set it unlimited: a stuck request holds a worker thread, and a few dozen can take the site down.

Friends & Partners