ASP 快速参考手册

W3Schools 的 ASP 快速参考。 打印出来,然后折叠在你的口袋里。


基本语法

ASP 脚本被 <% 和 %> 包围; 将一些输出写入浏览器:

<html>
<body>
<% response.write("Hello World!") %>
</body>
</html>

ASP 中的默认语言是 VBScript。 要使用另一种脚本语言,请在 ASP 页面顶部插入语言规范:

<%@ language="javascript" %>
<html>
<body>

<%
....
%>


表单和用户输入

Request.QueryString 用于以method="get" 的形式收集值。 使用 GET 方法从表单发送的信息对每个人都是可见的(它将显示在浏览器的地址栏中),并且对发送的信息量有限制。

Request.Form 用于以method="post" 的形式收集值。 使用 POST 方法从表单发送的信息对其他人是不可见的,并且对发送的信息量没有限制。


ASP Cookies

cookie 通常用于识别用户。 cookie 是服务器嵌入在用户计算机上的一个小文件。 每次同一台计算机通过浏览器请求一个页面时,它也会发送 cookie。

Response.Cookies 命令用于创建 cookie:

<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires="May 10,2002"
%>

注释: Response.Cookies 命令必须出现在 <html> 标记之前!

"Request.Cookies" 命令用于检索 cookie 值:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>



包括文件

您可以使用#include 指令在服务器执行之前将一个ASP 文件的内容插入到另一个ASP 文件中。 #include 指令用于创建将在多个页面上重复使用的函数、页眉、页脚或元素

语法:

<!--#include virtual="somefile.inc"-->
or
<!--#include file ="somefile.inc"-->

Use the virtual keyword to indicate a path beginning with a virtual directory. If a file named "header.inc" resides in a virtual directory named /html, the following line would insert the contents of "header.inc":

<!-- #include virtual ="/html/header.inc" -->

Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file. If you have a file in the html directory, and the file "header.inc" resides in html\headers, the following line would insert "header.inc" in your file:

<!-- #include file ="headers\header.inc" -->

Use the file keyword with the syntax (..\) to include a file from a higher-level directory.


Global.asa

Global.asa 文件是一个可选文件,它可以包含对象、变量和方法的声明,ASP 应用程序中的每个页面都可以访问这些声明。

注释: Global.asa 文件必须存放在 ASP 应用程序的根目录下,每个应用程序只能有一个 Global.asa 文件。

Global.asa 文件只能包含以下内容:

  • 应用事件
  • 会话事件
  • <object> 声明
  • 类型库声明
  • #include 指令

应用程序和会话事件

在 Global.asa 中,您可以告诉应用程序和会话对象在应用程序/会话启动时做什么以及在应用程序/会话结束时做什么。 用于此的代码放置在事件处理程序中。

注释: 我们不使用 <% 和 %>,要在 Global.asa 文件中插入脚本,我们必须将子例程放在 HTML <script> 标签内:

<script language="vbscript" runat="server">
sub Application_OnStart
  ' some code
end sub
sub Application_OnEnd
  ' some code
end sub
sub Session_OnStart
  ' some code
end sub
sub Session_OnEnd
  ' some code
end sub
</script>

<object> Declarations

It is also possible to create objects with session or application scope in Global.asa by using the <object> tag. 注释: The <object> tag should be outside the <script> tag!

Syntax:

<object runat="server" scope="scope" id="id"
{progid="progID"|classid="classID"}>
.......
</object>

TypeLibrary Declarations

A TypeLibrary is a container for the contents of a DLL file corresponding to a COM object. By including a call to the TypeLibrary in the Global.asa file, the constants of the COM object can be accessed, and errors can be better reported by the ASP code. If your Web application relies on COM objects that have declared data types in type libraries, you can declare the type libraries in Global.asa.

Syntax:

 <!--METADATA TYPE="TypeLib"
file="filename"
uuid="typelibraryuuid"
version="versionnumber"
lcid="localeid"
-->


Session 会话对象

Session 对象用于存储有关用户会话的信息或更改用户会话的设置。 存储在 Session 对象中的变量保存有关单个用户的信息,并且可用于一个应用程序中的所有页面。

Collections 集合

  • Contents - 保存使用脚本命令添加到会话中的每个项目
  • StaticObjects - 保存添加到带有 <object> 标记的会话的每个对象,以及给定的会话
  • Contents.Remove(item/index) - 从 Contents 集合中删除一个项目
  • Contents.RemoveAll() - 从 Contents 集合中删除每个项目

Properties 属性

  • CodePage - 设置将用于显示动态内容的代码页
  • LCID - 设置用于显示动态内容的区域标识符
  • SessionID - 返回会话 ID
  • 超时 - 设置会话的超时时间

Method 方法

  • Abandon - 杀死会话对象中的所有对象

Application 应用程序对象

一组协同工作以执行某些目的的 ASP 文件称为应用程序。 ASP 中的 Application 对象用于将这些文件联系在一起。 所有用户共享一个应用程序对象。 Application 对象应包含应用程序中许多页面将使用的信息(如数据库连接信息)。

Collections 集合

  • Contents - 保存使用脚本命令添加到应用程序的每个项目
  • StaticObjects - 使用 <object> 保存添加到应用程序的每个对象。 标记
  • Contents.Remove - 从集合中删除项目
  • Contents.RemoveAll - 删除集合中的每个项目

Methods 方法

  • Lock - 防止用户更改应用程序对象属性
  • Unlock - 允许用户更改应用程序对象属性

Response 响应对象

响应对象用于从服务器向用户发送输出。

Collection

  • Cookies(name) - 设置 cookie 值。 如果 cookie 不存在,则会创建它,并取指定的值

Properties

  • Buffer - 是否缓冲输出。当输出被缓冲时,服务器将保留响应,直到所有服务器脚本都被处理,或者直到脚本调用 Flush 或 End 方法。如果设置了这个属性,它应该在 <html> 之前。 ASP 文件中的标记
  • CacheControl - 设置代理服务器是否可以缓存输出。设置为Public时,输出可以被代理服务器缓存
  • Charset(charset_name) - 将字符集的名称(如 "ISO8859-1")设置为内容类型标题
  • ContentType - 设置 HTTP 内容类型(如 "text/html","image/gif", "image/jpeg", "text/plain")。默认为 "text/html"
  • Expires - 设置页面在浏览器上缓存多长时间后过期
  • ExpiresAbsolute - 设置缓存在浏览器上的页面过期的日期和时间
  • IsClientConnected - 检查客户端是否仍连接到服务器
  • Pics(pics_label) - 将值添加到图片标签响应标头
  • Status - 指定状态行的值

Methods

  • AddHeader(name, value) - 添加具有指定值的 HTML 标头
  • AppendToLog string - 在服务器日志条目的末尾添加一个字符串
  • BinaryWrite(data_to_write) - 写入给定信息而不进行任何字符集转换
  • Clear - 清除缓冲输出。 使用此方法处理错误。 如果 Response.Buffer 没有设置为 true,这个方法会导致运行时错误
  • End - 停止处理脚本,并返回当前结果
  • Flush - 立即发送缓冲输出。 如果 Response.Buffer 没有设置为 true,这个方法会导致运行时错误
  • Redirect(url) - 将用户重定向到另一个 url
  • Write(data_to_write) - 向用户写入文本

Request 请求对象

当浏览器向服务器请求页面时,称为请求。 Request 对象用于从用户那里获取信息。

Collection

  • ClientCertificate - 保存存储在客户端证书中的字段值
  • Cookies(name) - 保存 cookie 值
  • Form(element_name) - 保存表单(输入)值。 表单必须使用 post 方法
  • QueryString(variable_name) - 在查询字符串中保存变量值
  • ServerVariables(server_variable) - 保存服务器变量值

Property

  • TotalBytes - 保存客户端在请求正文中发送的总字节数

Method

  • BinaryRead - 获取作为发布请求的一部分从客户端发送到服务器的数据

Server 服务器对象

服务器对象用于访问服务器上的属性和方法。

Property

  • ScriptTimeout - 设置脚本在终止前可以运行多长时间

Method

  • CreateObject(type_of_object) - 创建一个对象的实例
  • Execute(path) - 从另一个 ASP 文件中执行一个 ASP 文件。 执行调用的 ASP 文件后,控制权返回到原来的 ASP 文件
  • GetLastError() - 返回一个描述发生错误的 ASPError 对象
  • HTMLEncode(string) - 将 HTML 编码应用于字符串
  • MapPath(path) - 将相对或虚拟路径映射到物理路径
  • Transfer(path) - 将所有状态信息发送到另一个 ASP 文件进行处理。 传输后,程序控制不会返回到原始 ASP 文件
  • URLEncode(string) - 将 URL 编码规则应用于字符串

来源:https://www.w3ccoo.com/asp/asp_quickref.asp