初級教程寫了七篇了,肯定還有一些初級的東西需要寫,我會慢慢的進行補充
中級教程的內(nèi)容:
這可能也是大家最關心的:如:數(shù)據(jù)庫的操作與封裝。Asp內(nèi)置對象的使用。這些部分我會花費較長的篇幅來說明,這一部分內(nèi)容需要你能夠比較熟練的使用ADO操作數(shù)據(jù)庫并且對asp的5大對象比較熟悉。
我們看一下網(wǎng)上比較流傳的一些資料:
眾所周知,ASP內(nèi)置了Response、Request、Server、session、application五個對象,其實這五個內(nèi)置對象正是IIS控制臺初始化的五個ActiveX DLL組件,既然IIS可以初始化這五個組件用于ASP中,我們當然也可以直接在我們的ActiveX DLL中引用這些組件來實現(xiàn)我們的編程,也就是說我們可以在VB應用程序中通過引用這些組件來實現(xiàn)訪問ASP內(nèi)置對象的功能。
只要你安裝了PWS4或者IIS4以上的WEB服務器,你就擁有了一個名稱叫做“Microsoft Active Server Pages Object”的對象庫,我們可以在VB的ActiveX DLL應用中引用這個對象庫,通過引用這個對象庫,我們就獲得了一個對象(類):ScriptingContext,這個對象也正是我們整個文章探討的核心對象。對象庫內(nèi)的關系如下:
對象庫 類 類成員
ASPTypeLibrary ScriptingContext Application
Request
Response
Session
Server
通過上面的關系圖,我們就可以很容易理解類ScriptingContent。
下面我們來看一個具體的例子吧:
打開vb6,新建Activex Dll工程。工程名修改為fCom,類名修改為fZ1
引用“Microsoft Active Server Pages Object”對象庫。
創(chuàng)建兩個組件事件:OnStartPage以及OnEndPage
在事件OnStartPage中創(chuàng)建類ScriptingContent的一個引用。
實例化類ScriptingContent。
代碼如下:
程序代碼
Option Explicit
'對象的聲明
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'當組件被創(chuàng)建的時候會觸發(fā)這個事件
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
myResponse.Write "ActiveX DLL組件已經(jīng)被創(chuàng)建了!"
End Sub
'當組件被銷毀的時候觸發(fā)這個事件
Public Sub OnEndPage()
myResponse.Write "ActiveX DLL組件已經(jīng)被銷毀!"
'銷毀對象
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'定義我們自己的一個組件方法
Public Sub HelloWorld()
myResponse.Write "這是用asp內(nèi)置對象寫的"
End Sub
測試
打開visual interdev6.0,生成一個asp文件
程序代碼
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
set obj=server.CreateObject("fCom.fZ1")
call obj.HelloWorld()
%>
</BODY>
</HTML>
配置好虛擬目錄,在ie中執(zhí)行此asp文件,得到結果如下:
ActiveX DLL組件已經(jīng)被創(chuàng)建了!這是用asp內(nèi)置對象寫的 ActiveX DLL組件已經(jīng)被銷毀!
Asp組件中級入門與精通系列之二
我們先來看看Application對象
以前使用Application對象常常用于計數(shù)器和數(shù)據(jù)庫的連接串
我們以計數(shù)器為例:
先看global.asa文件,這個比較簡單
程序代碼
<script language =vbscript runat=server>
sub Application_onstart
Application("Counter")=0
end sub
</script>
然后
打開vb6,新建Activex Dll工程。工程名修改為fCom,類名修改為fZ2
引用“Microsoft Active Server Pages Object”對象庫。
創(chuàng)建兩個組件事件:OnStartPage以及OnEndPage
在事件OnStartPage中創(chuàng)建類ScriptingContent的一個引用。
實例化類ScriptingContent。
代碼如下:
程序代碼
Option Explicit
'對象的聲明
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'當組件被創(chuàng)建的時候會觸發(fā)這個事件
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
'當組件被銷毀的時候觸發(fā)這個事件
Public Sub OnEndPage()
'銷毀對象
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'可以看到,把以前asp中寫的搬到了vb中,寫法是一樣的
Public Sub ShowCounter()
Dim intcounter As Long
myApplication.Lock
intcounter = myApplication("counter")
intcounter = intcounter + 1
myApplication("counter") = intcounter
myApplication.UnLock
myResponse.Write CStr(intcounter)
End Sub
測試
打開visual interdev6.0,生成一個asp文件
程序代碼
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim obj
set obj=server.CreateObject("fCom.fZ2")
obj.ShowCounter()
%>
</BODY>
</HTML>
配置好虛擬目錄,需要將global.asa文件放到根目錄下,在ie中執(zhí)行此asp文件,刷新頁面,就可以看到一個不斷變化的數(shù)字。
Application的用法就講到這里。
Asp組件中級入門與精通系列之三
Session相比較就簡單多了
打開vb6,新建Activex Dll工程。工程名修改為fCom,類名修改為fZ3
引用“Microsoft Active Server Pages Object”對象庫。
創(chuàng)建兩個組件事件:OnStartPage以及OnEndPage
在事件OnStartPage中創(chuàng)建類ScriptingContent的一個引用。
實例化類ScriptingContent。
代碼如下:
程序代碼
Option Explicit
'對象的聲明
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'當組件被創(chuàng)建的時候會觸發(fā)這個事件
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
'當組件被銷毀的時候觸發(fā)這個事件
Public Sub OnEndPage()
'銷毀對象
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'可以看到,把以前asp中寫的搬到了vb中,寫法是一樣的
'得到所有的session的變量和值
Public Sub ShowSession()
'可以設置超時20分鐘
mySession.Timeout = 20
Dim myitem
'得到所有的session
For Each myitem In mySession.Contents
myResponse.Write myitem & ": " & mySession.Contents(myitem)
myResponse.Write "<br>"
Next
End Sub
測試
打開visual interdev6.0,生成一個asp文件
配置好虛擬目錄,在ie中執(zhí)行此asp文件,可以看到
name: 龍卷風
age: 26
特長: 組件
Session的用法就講到這里。Session其他的用法類似。
Asp組件中級入門與精通系列之四
我們學習來Request
看如何在組件中得到頁面提交的內(nèi)容
打開vb6,新建Activex Dll工程。工程名修改為fCom,類名修改為fZ4
引用“Microsoft Active Server Pages Object”對象庫。
創(chuàng)建兩個組件事件:OnStartPage以及OnEndPage
在事件OnStartPage中創(chuàng)建類ScriptingContent的一個引用。
實例化類ScriptingContent。
代碼如下:
程序代碼
Option Explicit
'對象的聲明
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'當組件被創(chuàng)建的時候會觸發(fā)這個事件
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
'當組件被銷毀的時候觸發(fā)這個事件
Public Sub OnEndPage()
'銷毀對象
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'可以看到,把以前asp中寫的搬到了vb中,寫法是一樣的
Public Sub ShowRequest()
Dim myitem
'Post方式的
For Each myitem In myRequest.Form
myResponse.Write myitem & ": " & myRequest.Form(myitem)
myResponse.Write "<br>"
Next
‘Get方式的
For Each myitem In myRequest.QueryString
myResponse.Write myitem & ": " & myRequest.QueryString(myitem)
myResponse.Write "<br>"
Next
'單個信息
myResponse.Write "其中一個信息是" & ": " & myRequest("username")
myResponse.Write "<br>"
End Sub
測試
打開visual interdev6.0,生成一個fz41.asp文件
程序代碼
<%@ Language=VBScript %>
<HTML>
<BODY>
<form action="fz4_result.asp" method="post">
<INPUT id=text1 name=username>
<INPUT id=text2 name=age>
<INPUT id=submit1 type=submit value=Submit name=提交>
</form>
</BODY>
</HTML>
還需要生成一個提交后的fz4_result.asp文件
程序代碼
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim obj
set obj=server.CreateObject("fCom.fZ4")
call obj.ShowRequest
%>
</BODY>
</HTML>
此外我們還要看一看Get方式提交的,所以需要一個fz42.asp文件
程序代碼
<%@ Language=VBScript %>
<HTML>
<BODY>
<form action="fz4_result.asp?username='"& username &"'& age='"& age &"'" method="get" id=form1 name=form1>
<INPUT id=text1 name=username>
<INPUT id=text2 name=age>
<INPUT id=submit1 type=submit value=Submit name=提交>
</form>
</BODY>
</HTML>
配置好虛擬目錄,在ie中執(zhí)行fc41.asp文件,輸入內(nèi)容后,點擊按鈕,可以看到
username: 龍卷風
age: 26
提交: Submit
其中一個信息是: 龍卷風
我們再來執(zhí)行在ie中執(zhí)行fc42.asp文件,輸入內(nèi)容后,點擊按鈕,可以看到
username: 龍卷風
age: 26
提交: Submit
其中一個信息是: 龍卷風
同時地址欄變成了
http://yang/xml/fz4_result.asp?username=%C1%FA%BE%ED%B7%E7&age=26&%CC%E1%BD%BB=Submit
未完待續(xù)
Asp組件中級入門與精通系列之五
我們學習來看一下Response對象。其實我們前面的教程中一直都在使用這個對象的Write方法。
這里我們用Response對象設置cookie。
打開vb6,新建Activex Dll工程。工程名修改為fCom,類名修改為fZ5
引用“Microsoft Active Server Pages Object”對象庫。
創(chuàng)建兩個組件事件:OnStartPage以及OnEndPage
在事件OnStartPage中創(chuàng)建類ScriptingContent的一個引用。
實例化類ScriptingContent。
代碼如下:
程序代碼
Option Explicit
'對象的聲明
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'當組件被創(chuàng)建的時候會觸發(fā)這個事件
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
'當組件被銷毀的時候觸發(fā)這個事件
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 "<br>"
Next
'單個信息
myResponse.Write "其中用戶姓名是" & ": " & myRequest.Cookies("username")
myResponse.Write "<br>"
myResponse.Write "其中用戶年齡是" & ": " & myRequest.Cookies("age")
myResponse.Write "<br>"
End Sub
'組件中設置cookie,頁面中得到
Public Sub SetCookie()
myResponse.Cookies("com_username") = "龍卷風"
myResponse.Cookies("com_age") = 26
myResponse.Expires = #9/13/2004#
End Sub
編譯成Dll文件,系統(tǒng)自動會注冊。
否則就手工注冊 Regsvr32 f:/test/fcom.dll
測試
打開visual interdev6.0,生成一個fz5.asp文件
配置好虛擬目錄,在ie中執(zhí)行fc5.asp文件,可以看到
龍卷風
26
age: 26
username: 龍卷風
com_age: 26
com_username: 龍卷風
其中用戶姓名是: 龍卷風
其中用戶年齡是: 26
未完待續(xù)
Asp組件中級入門與精通系列之六
作為Asp的內(nèi)置對象,我們最后來學習Server對象
Server對象用的比較多的就是Html編碼,Url編碼和網(wǎng)頁的重定向,傳送。
打開vb6,新建Activex Dll工程。工程名修改為fCom,類名修改為fZ6
引用“Microsoft Active Server Pages Object”對象庫。
創(chuàng)建兩個組件事件:OnStartPage以及OnEndPage
在事件OnStartPage中創(chuàng)建類ScriptingContent的一個引用。
實例化類ScriptingContent。
代碼如下:
程序代碼
Option Explicit
'對象的聲明
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'當組件被創(chuàng)建的時候會觸發(fā)這個事件
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
'當組件被銷毀的時候觸發(fā)這個事件
Public Sub OnEndPage()
'銷毀對象
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
Public Sub ShowHtml(ByVal strHtml As String)
myResponse.Write myServer.HTMLEncode(strHtml)
End Sub
Public Sub ShowUrl(ByVal strUrl As String)
myResponse.Write myServer.URLEncode(strUrl)
End Sub
Public Sub ExecuteUrl()
myServer.Transfer "fz5.asp"
End Sub
編譯成Dll文件,系統(tǒng)自動會注冊。
否則就手工注冊 Regsvr32 f:/test/fcom.dll
測試
打開visual interdev6.0,生成一個fz6.asp文件
配置好虛擬目錄,在ie中執(zhí)行fc6.asp文件可以看到
呵呵 測試一下
name=Mrs+%C1%FA%BE%ED%B7%E7&age=26
可以使用IE的查看源文件來看HTML編碼
ASP的內(nèi)置對象就暫時介紹到這里,后面我們還會陸續(xù)的學習。
大家也可以舉一反三,學習沒有介紹到的屬性和方法。
Asp組件中級入門與精通系列之七
開始數(shù)據(jù)庫操作。
常見的組件封裝
1.把數(shù)據(jù)庫的連接信息封裝起來。
1> 直接返回數(shù)據(jù)庫連接串,如,組件中
程序代碼
Public Function datasource() As Variant
datasource = "driver={sql server};server=yang;uid=sa;pwd=; database=northwind"
End Function
asp調(diào)用
程序代碼
set obj=server.CreateObject("webdb.getinfo")
oconn=obj.datasource()
這樣的缺點是很明顯的,在asp文件中,直接response.write oconn即可顯示出數(shù)據(jù)庫連接串,并沒有起到預期的作用。
2> 返回adodb.connection對象
程序代碼
Public Function GetConn() As ADODB.Connection
Set conn = New ADODB.Connection
conn.ConnectionString = "PRovider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;passWord=;Initial Catalog=Northwind;Data Source=yang"
conn.Open
Set GetConn = conn
End Function
Asp調(diào)用
程序代碼
Dim DataQuery
Set DataQuery=Server.CreateObject("WebDbtest.GetInfomation")
set rs=server.createobject("adodb.recordset")
sql="select * from employees"
Rs.open sql,DataQuery.getconn,1,3
response.Write Rs("LastName")
Response.write DataQuery.getconn.ConnectionString
set Rs=nothing
這樣看起來不錯,只是Response.write DataQuery.getconn.ConnectionString還是會顯示出數(shù)據(jù)庫連接串,大家可以測試。
2.將組件封裝到記錄集
可以看一下前段時間寫的http://blog.csdn.net/online/archive/2003/12/11/7764.aspx
這段代碼不好的一點就是數(shù)據(jù)庫的連接放到了頁面中判斷,連接成功后,才開始訪問數(shù)據(jù),個人認為,最好的做法是:
封裝到記錄集,組件方法中連接數(shù)據(jù)庫,操作完后,及時關閉
盡量在組件中生成HTML代碼,做到全部封裝。如下面的這種方式
而不是部分的封裝。
Asp組件中級入門與精通系列之八
這段時間一直比較忙,呵呵,今天我們來看一下一個完整的數(shù)據(jù)封裝的、帶分頁的例子
打開vb6,新建Activex Dll工程。工程名修改為fCom,類名修改為fZ8
引用“Microsoft Active Server Pages Object”,”Microsoft Activex Data Object 2.7 Library”對象庫。
創(chuàng)建兩個組件事件:OnStartPage以及OnEndPage
在事件OnStartPage中創(chuàng)建類ScriptingContent的一個引用。
實例化類ScriptingContent。
代碼如下:
程序代碼
Option Explicit
'**************************************************
'作者:龍卷風
'功能:簡單的可以定制的,完全封裝的組件
'時間:2005-01-01
'**************************************************
'對象的聲明
Dim MyResponse As Response
Dim MyRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'私有變量
Private mPageSize As Long
Private mstrSql As String
'當組件被創(chuàng)建的時候會觸發(fā)這個事件
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
'當組件被銷毀的時候觸發(fā)這個事件
Public Sub OnEndPage()
'銷毀對象
Set MyResponse = Nothing
Set MyRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'顯示Table
Public Function ShowTable()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim i As Integer
Dim j As Integer
Dim intPage As Integer
Dim intPageCount As Integer
Dim strScriptName As String
Dim intPos As Integer
Dim intFieldCount As Integer
'得到路徑
strScriptName = MyRequest.ServerVariables("Script_Name")
intPos = InStrRev(strScriptName, "/")
If intPos <> 0 Then
strScriptName = Mid(strScriptName, intPos + 1)
End If
If IsEmpty(MyRequest("page")) Then
intPage = 1
Else
intPage = CInt(MyRequest("page"))
End If
On Error GoTo err
conn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind;Data Source=localhost"
rs.Open mstrSql, conn, adOpenStatic, adLockReadOnly
'得到記錄數(shù)
intFieldCount = rs.Fields.Count
'輸出表格
MyResponse.Write "<table border=1 cellspacing=0 cellpadding=2>"
If Not rs.EOF Then
rs.PageSize = mPageSize
rs.AbsolutePage = intPage
'得到頁數(shù)
intPageCount = rs.PageCount
'處理分頁
If intPage < 1 Then intPage = 1
If intPage > intPageCount Then intPage = intPageCount
'輸出表頭
MyResponse.Write "<tr>"
For i = 0 To intFieldCount - 1
MyResponse.Write "<th>" & rs(i).Name & "</th>"
Next
MyResponse.Write "</tr>"
'輸出內(nèi)容
For i = 1 To mPageSize
If rs.EOF Then
Exit For
End If
MyResponse.Write "<tr>"
For j = 0 To intFieldCount - 1
MyResponse.Write "<td>" & rs.Fields(j).Value & "</td>"
Next
MyResponse.Write "</tr>"
rs.MoveNext
Next
'輸出分頁
MyResponse.Write "<tr>"
If intPage <> 1 Then
MyResponse.Write "<a href=" & strScriptName & "?page=1>[第一頁]</a>"
MyResponse.Write "<a href=" & strScriptName & "?page=" & intPage - 1 & " >[上一頁]</a>"
End If
If intPage <> intPageCount Then
MyResponse.Write "<a href=" & strScriptName & "?page=" & intPage + 1 & ">[下一頁]</a>"
MyResponse.Write "<a href=" & strScriptName & "?page=" & intPageCount & ">[最后一頁]</a>"
End If
MyResponse.Write "頁次:<FONT COLOR='Red'>" & intPage & "/ " & intPageCount & "</FONT>"
MyResponse.Write "</tr>"
End If
MyResponse.Write "</table>"
'釋放資源
If Not rs Is Nothing Then
If rs.State = 1 Then
rs.Close
End If
Set rs = Nothing
End If
If Not conn Is Nothing Then
If conn.State = 1 Then
conn.Close
End If
Set conn = Nothing
End If
Exit Function
err:
MyResponse.Write err.Number & err.Description
If Not rs Is Nothing Then
If rs.State = 1 Then
rs.Close
End If
Set rs = Nothing
End If
If Not conn Is Nothing Then
If conn.State = 1 Then
conn.Close
End If
Set conn = Nothing
End If
End Function
'定義屬性
Public Property Get ShowPageSize() As Variant
ShowPageSize = mPageSize
End Property
Public Property Let ShowPageSize(ByVal vNewValue As Variant)
mPageSize = vNewValue
End Property
Public Property Get strSQL() As Variant
strSQL = mstrSql
End Property
Public Property Let strSQL(ByVal vNewValue As Variant)
mstrSql = vNewValue
End Property
編譯成Dll文件,系統(tǒng)自動會注冊。
否則就手工注冊 Regsvr32 f:/test/fcom.dll
測試
打開visual interdev6.0,生成一個fz8.asp文件
程序代碼
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim obj
set obj=server.CreateObject("fcom.fz8")
'每頁顯示的記錄數(shù)
obj.ShowPageSize=10
'顯示的sql語句
obj.strSQL="select customerid,companyname,contactname,contacttitle,address from customers"
obj.ShowTable()
%>
</BODY>
</HTML>
新聞熱點
疑難解答