Asp组件中级入门与精通系列之五

我们学习来看一下 Response 对象。其实我们前面的教程中一直都在使用这个对象的 Write 方法。

这里我们用 Response 对象设置 cookie 。

?

打开 vb6, 新建 Activex Dll 工程。工程名修改为 fCom, 类名修改为 fZ5
引用 “Microsoft Active Server Pages Object” 对象库。
创建两个组件事件: OnStartPage 以及 OnEndPage
在事件 OnStartPage 中创建类 ScriptingContent 的一个引用。
实例化类 ScriptingContent 。

?

代码如下:

Option Explicit

' 对象的声明

Dim myResponse As Response

Dim myRequest As Request

Dim myApplication As Application

Dim myServer As Server

Dim mySession As Session

?

??? ' 当组件被创建的时候会触发这个事件

Public Sub OnStartPage(myScriptingContent As ScriptingContext)

???? ' 进行对象的实例化

???? Set myResponse = myScriptingContent.Response

???? Set myRequest = myScriptingContent.Request

???? Set myServer = myScriptingContent.Server

???? Set myApplication = myScriptingContent.Application

???? Set mySession = myScriptingContent.Session

End Sub

?

??? ' 当组件被销毁的时候触发这个事件

Public Sub OnEndPage()

???? ' 销毁对象

???? Set myResponse = Nothing

???? Set myRequest = Nothing

???? Set myServer = Nothing

???? Set myApplication = Nothing

???? Set mySession = Nothing

End Sub

?

' 从页面中设置 Cookie, 组件中得到

Public Sub GetCookie()

??? Dim myitem

??? ' 全部信息

??? For Each myitem In myRequest.Cookies

??????? myResponse.Write myitem & ": " & myRequest.Cookies.Item(myitem)

??????? myResponse.Write "
"

??? Next

???

??? ' 单个信息

??? myResponse.Write " 其中用户姓名是 " & ": " & myRequest.Cookies("username")

??? myResponse.Write "
"

??? myResponse.Write " 其中用户年龄是 " & ": " & myRequest.Cookies("age")

??? myResponse.Write "
"

End Sub

' 组件中设置 cookie, 页面中得到

Public Sub SetCookie()

??? myResponse.Cookies("com_username") = " 龙卷风 "

??? myResponse.Cookies("com_age") = 26

??? myResponse.Expires = #9/13/2004#

End Sub

?

编译成 Dll 文件 , 系统自动会注册。

否则就手工注册 Regsvr32 f:\test\fcom.dll

?

测试

打开 visual interdev6.0, 生成一个 fz5.asp 文件

1@ Language=VBScript 
 1 
 2
 3dim obj 
 4
 5set obj=server.CreateObject ("fcom.fz5") 
 6
 7call obj.setcookie() 
 8
 9Response.Write Request.Cookies("com_username") 
10
11Response.Write "   
12" 
13
14Response.Write Request.Cookies("com_age")  ??? 
15
16Response.Write "   
17" 
18
19? 
20
21'  下面在页面中设置  Cookie 
22
23Response.Cookies("username") = "  龙卷风  " 
24
25Response.Cookies("age") = 26 
26
27call obj.GetCookie() 
28
29? 

?

配置好虚拟目录,在 ie 中执行 fc5.asp 文件,可以看到

龙卷风
26
age: 26
username: 龙卷风
com_age: 26
com_username: 龙卷风
其中用户姓名是 : 龙卷风
其中用户年龄是 : 26

未完待续

Published At
Categories with Web编程
Tagged with
comments powered by Disqus