在 ASP 的内建对象中除了用于发送、接收和处理数据的对象外,还有一些非常实用的代表 Active Server 应用程序和单个用户信息的对象。
   让我们先来看看 Application 对象。在同一虚拟目录及其子目录下的所有 .asp 文件构成了 ASP 应用程序。我们非但可以使用 Application 对象,在给定的应用程序的所有用户之间共享信息,并在服务器运行期间持久的保存数据。而且,Application 对象还有控制访问应用层数据的方法和可用于在应用程序启动和停止时触发过程的事件。
   下面就让我们一起来学习 Application 对象。   一、属性

   虽然 Application 对象没有内置的属性,但我们可以使用以下句法设置用户定义的属性也可称为集合。

   Application(" 属性 / 集合名称 ")= 值

   我们可以使用如下脚本声明并建立 Application 对象的属性。
< %
Application("MyVar") = "Hello"
Set Application("MyObj") = Server.CreateObject("MyComponent")
%>

 

   一旦我们分配了 Application 对象的属性,它就会持久地存在,直到关闭 WEB 服务器服务使得 Application 停止。由于存储在 Application 对象中的数值可以被应用程序的所有用户读取,所以 Application 对象的属性特别适合在应用程序的用户之间传递信息。
   二、方法

   Application 对象有两个方法,它们都是用于处理多个用户对存储在 Application 中的数据进行写入的问题

   1、Lock 方法禁止其他客户修改 Application 对象的属性。

   Lock 方法阻止其他客户修改存储在 Application 对象中的变量,以确保在同一时刻仅有一个客户可修改和存取 Application 变量。如果用户没有明确调用 Unlock 方法,则服务器将在 .asp 文件结束或超时后即解除对 Application 对象的锁定。

   让我们来看看下面这段用 Application 来记录页面访问次数的程序 :
< %
Dim NumVisitsNumVisits=0
Application.LockApplication("NumVisits") = Application("NumVisits") + 1
Application.Unlock
%>
欢迎光临本网页,你是本页的第 < %= Application("NumVisits") %> 位访客 !
   将以上脚本保存在你的 .asp 文件中,就轻而易举地给你的页面添加了一个计数器。

   2、和 Lock 方法相反,Unlock 方法允许其他客户修改 Application 对象的属性。

   在上面的例子中,上述例子中,Unlock 方法解除对象的锁定,使得下一个客户端能够增加 NumVisits 的值。

   三、事件

   1、Application_OnStart

   Application_OnStart 事件在首次创建新的会话 ( 即 Session_OnStart 事件 ) 之前发生。当 WEB 服务器启动并允许对应用程序所包含的文件进行请求时就触发 Application_OnStart 事件。Application_OnStart 事件的处理过程必须写在 Global.asa 文件之中。

   Application_OnStart 事件的语法如下 :
< SCRIPT LANGUAGE=ScriptLanguage RUNAT=Server>
Sub Application_OnStart. . .
End Sub
< /SCRIPT>
   2、Application_OnEnd

   Application_OnEnd 事件在应用程序退出时于 Session_OnEnd 事件之后发生,Application_OnEnd 事件的处理过程也必须写在 Global.asa 文件之中。

   下面让我们来看看在使用 Application 对象时必须注意的一些事项。

   不能在 Application 对象中存储 ASP 内建对象。例如,下面的每一行都返回一个错误。
< %
Set Application("var1")=Session
Set Application("var2")=Request
Set Application("var3")=Response
Set Application("var4")=Server
Set Application("var5")=Application
Set Application("var6")=ObjectContext
%>
   若您将一个数组存储在 Application 对象中,请不要直接更改存储在数组中的元素。例如,下列的脚本无法运行。
< % Application("StoredArray")(3) = "new value" %> Read the rest of this entry

ASP内置函数

1.函数array() 
功能:创建一个数组变量 
格式:array(list) 
参数:list 为数组变量中的每个数值列,中间用逗号间隔 
例子: 
<% i = array ("1","2","3") %> 
结果: i 被赋予为数组 2.函数Cint() 
功能:将一表达式/其它类型的变量转换成整数类型(int) 
格式:Cint(expression) 
参数:expression 是任何有效的表达式/其它类型的变量 
例子: 
<% 
f = "234" 
response.write cINT(f) + 2 
%> 
结果: 236 
函数Cint()将字符"234"转换 成整数234.如果表达式为空, 或者无效时,返回值为0; 

3.函数:Creatobject() 
功能:创建及返回一个ActiveX对象. 
格式:Creatobject(obname) 
参数bname 是对象的名称 
例子: 
<% 
Set con = Server.CreateObject("ADODB.Connection") 
%> 
结果: 

4.函数Cstr() 
功能:将一表达式/其它类型的变量转换成字符类型(string) 
格式:Cstr(expression) 
参数:expression是任何有效的表达式/其它类型的变量 
例子: 
<% 
s = 3 + 2 
response.write "The result is:" & cStr(s) 
%> 
结果:函数Cstr()将整数 5 转换 成字符"5". 

5.函数Date() 
功能:返回当前系统(server端)的日期 
格式: Date() 
参数:无 
例子<% date () %> 
结果:05/10/00 

6.函数Dateadd() 
功能:计算某个指定的时间和 
格式: dateadd(timeinterval,number,date) 
参数:timeinterval是时间单位(月,日..); number是时间间隔值,date是时间始点. 
例子: 
<% 
currentDate = #8/4/99# 
newDate = DateAdd("m",3,currentDate) 
response.write newDate 
%> <% 
currentDate = #12:34:45 PM# 
newDate = DateAdd("h",3,currentDate) 
response.write newDate 
%> 
结果: 
11/4/99 
3:34:45 PM 
其中 
"m" = "month"; 
"d" = "day"; 
如果是currentDate 格式,则, 
"h" = "hour"; 
"s" = "second"; 

7.函数Datediff() 
功能:计算某量个指定的时间差 
格式: datediff(timeinterval,date1,date2[,firstdayofweek[,firstdayofyear]]) 
参数: timeinterval 是时间单位; date1,date2是有效的日期表达式,firstdayofweek,firstdayofyear 是任意选项. 
例子: 
<% 
fromDate = #8/4/99# 
toDate = #1/1/2000# 
response.write "There are " & _ 
DateDiff("d",fromDate,toDate) & _ 
" days to millenium from 8/4/99." 
%> 
结果:There are 150 days to millenium from 8/4/99. 

8.函数day() 
功能:返回一个整数值,对应于某月的某日 
格式: day(date) 
参数: date是一个有效的日期表达式; 
例子<% =date(#8/4/99#) %> 
结果:4 

9.函数formatcurrency() 
功能:转换成货币格式 
格式: formatcurrency(expression [,digit[,leadingdigit[,paren[,groupdigit]]]]) 
参数: expression 是有效的数字表达式;digit表示小数点后的位数;leadingdigit,paren,groupdigit是任意选项. 
例子<%=FormatCurrency(34.3456)%> 
结果34.35 

10.函数Formatdatetime() 
功能:格式化日期表达式/变量 
格式: formatdatetime(date[,nameformat]) 
参数: date为有效的日期表达式/变量;nameformat是指定的日期格式常量名称. 
例子<% =formatdatetime("08/04/99",vblongdate) %> 
结果:Wednesday,August 04,1999 
说明: 
--------------------------------------------------------------------------------
描述
返回表达式,此表达式已被格式化为日期或时间。 
语法
FormatDateTime(Date[, NamedFormat])
FormatDateTime 函数的语法有以下参数:

参数 描述 
Date 必选项。要被格式化的日期表达式。 
NamedFormat 可选项。指示所使用的日期/时间格式的数值,如果省略,则使用 vbGeneralDate。 

设置
NamedFormat 参数可以有以下值:
常数 值 描述 
vbGeneralDate 0 显示日期和/或时间。如果有日期部分,则将该部分显示为短日期格式。如果有时间部分,则将该部分显示为长时间格式。如果都存在,则显示所有部分。 
vbLongDate 1 使用计算机区域设置中指定的长日期格式显示日期。 
vbShortDate 2 使用计算机区域设置中指定的短日期格式显示日期。 
vbLongTime 3 使用计算机区域设置中指定的时间格式显示时间。 
vbShortTime 4 使用 24 小时格式 (hh:mm) 显示时间。 

说明
下面例子利用 FormatDateTime 函数把表达式格式化为长日期型并且把它赋给 MyDateTime: 
Function GetCurrentDate 
"FormatDateTime 把日期型格式化为长日期型。
GetCurrentDate = FormatDateTime(Date, 1) 
End Function
--------------------------------------------------------------------------------
[Ctrl+A 全部选择 提示:您可先修改部分代码,再按运行]

11.函数Isnumeric() 
功能:返回一个布尔值,判断变量是否为数字变量,或者是可以转换成数字的其它变量. 
格式:isnumeric(expression) 
参数:expression 是任意的变量. 
例子: 
<% 
i="234" 
response.write isnumeric(i) 
%> 
结果: true. 

12.函数Isobject() 
功能:返回一个布尔值,判断变量是否为对象的变量, 
格式: isobject(expression) 
参数: expression 是任意的变量. 
例子: 
<% 
set con =server.creatobject("adodb.connection") 
response.write isobject(con) 
%> 
结果: true 

13.函数:Lbound() 
功能:返回一个数组的下界. 
格式:Lbound(arrayname[,dimension]) 
参数:arrayname 是数组变量,dimension 是任意项 
例子: 
<% 
i = array("1","2","3") 
response.write lbound(i) 
%> 
结果:0

14.函数Lcase() 
功能:将一字符类型变量的字符全部变换小写字符. 
格式:Lcase(string) 
参数:string是字符串变量 
例子: 
<% 
str="THIS is Lcase!" 
response.write Lcase(str) 
%> 
结果:this is lcase! 

15.函数left() 
功能:截取一个字符串的前部分; 
格式:left(string,length) 
参数:string字符串,length截取的长度. 
例子: <% =left("this is a test!",6) %> 
结果:this i 

16.函数len() 
功能:返回字符串长度或者变量的字节长度 
格式:len(string *varname) 
参数:string字符串;varname任意的变量名称 
例子: 
<% 
strtest="this is a test!" 
response.write len(strtest) 
%> 
结果:15  Read the rest of this entry

用ASP压缩ACCESS数据库

<%
' 功能:压缩ACCESS数据库
Sub CompactDB(strDBFileName)
Dim strOldDB,strNewDB,objFSO
strOldDB = server.MapPath(strDBFileName)
strNewDB = server.MapPath("New" &amp; strDBFileName)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strOldDB) Then
' 压缩数据库
Set Engine = Server.CreateObject("JRO.JetEngine")
strPvd = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Engine.CompactDatabase strPvd &amp; strOldDB , strPvd &amp; strNewDB
Set Engine = Nothing
' 删除旧的数据库文件
objFSO.DeleteFile strOldDB
' 将压缩好的数据库文件拷贝回来
objFSO.MoveFile strNewDB, strOldDB
Response.Write("数据库压缩完毕!")
Else
Response.Write("找不到指定的数据库文件!")
End If
Set objFSO = Nothing
End Sub
%>
 
'调用方式:
 
Call CompactDB("Database.mdb")

遍历Request.ServerVariables

Request.ServerVariables("Url")
返回服务器地址

Request.ServerVariables("Path_Info")
客户端提供的路径信息

Request.ServerVariables("Appl_Physical_Path")
与应用程序元数据库路径相应的物理路径

Request.ServerVariables("Path_Translated")
通过由虚拟至物理的映射后得到的路径

Request.ServerVariables("Script_Name")
执行脚本的名称

Request.ServerVariables("Query_String")
查询字符串內容

Request.ServerVariables("Http_Referer")
请求的字符串內容

Request.ServerVariables("Server_Port")
接受请求的服务器端口号

Request.ServerVariables("Remote_Addr")
发出请求的远程主机的IP地址

Request.ServerVariables("Remote_Host")
发出请求的远程主机名称

Request.ServerVariables("Local_Addr")
返回接受请求的服务器地址

Request.ServerVariables("Http_Host")
返回服务器地址

Request.ServerVariables("Server_Name")
服务器的主机名、DNS地址或IP地址

Request.ServerVariables("Request_Method")
提出请求的方法比如GET、HEAD、POST等等

Request.ServerVariables("Server_Port_Secure")
如果接受请求的服务器端口为安全端口时,则为1,否则为0

Request.ServerVariables("Server_Protocol")
服务器使用的协议的名称和版本

Request.ServerVariables("Server_Software")
应答请求并运行网关的服务器软件的名称和版本

Request.ServerVariables("All_Http")
客户端发送的所有HTTP标头,前缀HTTP_

Request.ServerVariables("All_Raw")
客户端发送的所有HTTP标头,其结果和客户端发送时一样,没有前缀HTTP_

Request.ServerVariables("Appl_MD_Path")
应用程序的元数据库路径

Request.ServerVariables("Content_Length")
客户端发出內容的长度

Request.ServerVariables("Https")
如果请求穿过安全通道(SSL),则返回ON如果请求来自非安全通道,则返回OFF

Request.ServerVariables("Instance_ID")
IIS实例的ID号

Read the rest of this entry

记录一下,ASP列出数据库所有表的方法

<%
strConn="DBQ="+server.mappath("lixiaopeng.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set objConn=server.createobject("Adodb.connection")
objConn.open strConn
set rsSchema=objConn.openSchema(20)
rsSchema.movefirst
Do Until rsSchema.EOF
if rsSchema("TABLE_TYPE")="TABLE" then
response.write rsSchema("TABLE_NAME") &amp; "
"
end if
rsSchema.movenext
Loop
set objConn=nothing
%>

至今还有这么多传统ASP (Classic ASP) 应用程序和开发者,真让我感到惊讶。我还记得96、97年的时候IIS3问世,我体验了最原始的ASP程序,并为它与ColdFusion相比表现出的惊人的可编程性赞叹不已。使用传统ASP我建立了许多应用, 它将成为我记忆中永不褪色的闪光点:)
使用传统ASP编程的开发人员应该对IIS7的几个小变化引起注意。

ASP缺省不安装

重要的事先来! 如果从XP迁移到Vista / Longhorn Server, 您也许会得到这个错误:
--------------------------------------------------------------------------------------------------------------------

HTTP Error 404.3 - Not Found

Description: The page you are requesting cannot be served because of the Multipurpose Internet Mail Extensions (MIME) map policy that is configured on the Web server. The page you requested has a file name extension that is not recognized, and is not allowed.

--------------------------------------------------------------------------------------------------------------------
这是您没有安装ASP组件时的情形,所以去您安装IIS的地方找到IIS/WWW Services(WWW服务)/Application Development(应用程序开发)/ASP 来安装它。:)

Access 与传统ASP

很多人使用Access作为数据库-因为它小巧,可复制, 容易处理。我们在Vista IIS7的一个变动就是缺省不使用ASP和Access。本篇我将详细描述这一变化, 但根本上这是因为应用程序池(Application Pool)缺省不再使用\windows\temp, 而改为应用程序池身份档案(identity's profile)和临时目录。并且因为只有"网络服务"才能向网络服务的临时目录执行写入操作, ASP使用虚拟身份(impersonated identity)访问数据库, 所以匿名或已认证的ASP应用程序就会中止。如果您在IIS7使用ASP+Access、你大概会看到与以下提示类似的错误信息:
--------------------------------------------------------------------------------------------------------------------

Microsoft JET Database Engine error '80004005'
Unspecified error

--------------------------------------------------------------------------------------------------------------------
回答很简单: 关闭loadUserProfile, 或者设置temp directory的访问权限以允许写操作. 这一问题和其它兼容性问题导致, 我们考虑在Longhorn Server / Vista SP1中取消这一变化.

不过现在, 您可以在下列措施中任选一项:

这个 appcmd 指令将为缺省应用程序池关闭 loadUserProfile. 假如您的程序运行于不同的应用程序池(AppPool), 就相应改动一下:

%windir%\system32\inetsrv\appcmd set config -section:applicationPools /[name='DefaultAppPool'].processModel.loadUserProfile:false

这条命令将把网络服务的临时目录权限改为可读可写。如果你是以另一个身份来运行程序,那么你还需要将那个身份的临时目录权限给打开:
icacls %windir%\serviceprofiles\networkservice\AppData\Local\Temp /grant Users:(CI)(S,WD,AD,X)

icacls %windir%\serviceprofiles\networkservice\AppData\Local\Temp /grant "CREATOR OWNER":(OI)(CI)(IO)(F)

脚本错误默认为不在浏览器中显示了

作为安全策略的一部分,我们关闭了ASP的自动在浏览器中显示脚本错误的提示。也就是说一般用户将不会再看到你的语句到底是哪一行出了差错。取而代之的是这个错误提示:
--------------------------------------------------------------------------------------------------------------------

An error occurred on the server when processing the URL. Please contact the system administrator

--------------------------------------------------------------------------------------------------------------------

要想恢复到IIS6的状态也很容易,运行下面命令即可:

%windir%\system32\inetsrv\appcmd set config -section:asp -scriptErrorSentToBrowser:true

或者你可以在窗口界面中找到以下这个选项:

 

iis7_1

之后你就能看到这样的错误提示信息了:
--------------------------------------------------------------------------------------------------------------------

Microsoft VBScript compilation error '800a03ea'

Syntax error

/test.asp, line 4

Read the rest of this entry