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

类别:.NET开发 点击:0 评论:0 推荐:

我们学习来看一下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文件

<%@ Language=VBScript %>

<%

dim obj

set obj=server.CreateObject ("fcom.fz5")

call obj.setcookie()

Response.Write Request.Cookies("com_username")

Response.Write "
"

Response.Write Request.Cookies("com_age")???

Response.Write "
"

?

'下面在页面中设置Cookie

Response.Cookies("username") = "龙卷风"

Response.Cookies("age") = 26

call obj.GetCookie()

?

%>

?

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

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

未完待续

本文地址:http://com.8s8s.com/it/it44121.htm