I have written seven primary tutorials,
one of the series of intermediate entry and mastery of Asp components
. There are definitely some basic things that need to be written. I will slowly supplementthe content of the intermediate tutorials:
This may also be what everyone is most concerned about: such as: database Operation and packaging. Use of Asp built-in objects. I will spend a long time explaining these parts. This part requires you to be more proficient in using ADO to operate the database and be familiar with the five major objects of ASP.
Let’s take a look at some of the information circulating online:
As we all know, ASP has five built-in objects: Response, Request, Server, Session, and Application. In fact, these five built-in objects are the five ActiveX DLL components initialized by the IIS console. Since IIS can initialize these five components for use in ASP, Of course, we can also directly reference these components in our ActiveX DLL to implement our programming, which means that we can achieve the function of accessing ASP built-in objects by referencing these components in VB applications.
As long as you install a WEB server with PWS4 or IIS4 or above, you will have an object library named "Microsoft Active Server Pages Object". We can reference this object library in VB's ActiveX DLL application. By referencing this object library, We have obtained an object (class): ScriptingContext, which is also the core object of our entire article. The relationship within the object library is as follows:
Object library class class member
ASPTypeLibrary ScriptingContext Application
Request
Response
Session
Server
Through the above relationship diagram, we can easily understand the class ScriptingContent.
Let's take a look at a specific example:
open vb6 and create a new Activex Dll project. Change the project name to fCom and the class name to fZ1
Reference to the "Microsoft Active Server Pages Object" object library.
Create two component events: OnStartPage and OnEndPage
Create a reference to class ScriptingContent in the event OnStartPage.
Instantiate class ScriptingContent.
The code is as follows:
program code
Option Explicit
'Object declaration
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'This event will be triggered when the component is created
Public Sub OnStartPage(myScriptingContent As ScriptingContext)
'Execute object instantiation
Set myResponse = myScriptingContent.Response
Set myRequest = myScriptingContent.Request
Set myServer = myScriptingContent.Server
Set myApplication = myScriptingContent.Application
Set mySession = myScriptingContent.Session
myResponse.Write "ActiveX DLL component has been created!"
End Sub
'This event is triggered when the component is destroyed
Public Sub OnEndPage()
myResponse.Write "ActiveX DLL component has been destroyed!"
'Destroy the object
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'Define a component method of our own
Public Sub HelloWorld()
myResponse.Write "This is written using asp built-in objects"
End Sub
Test open visual interdev6.0 and generate an asp file
program code
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
set obj=server.CreateObject("fCom.fZ1")
call obj.HelloWorld()
%>
</BODY>
</HTML>
Configure the virtual directory and execute this asp file in IE. The results are as follows:
The ActiveX DLL component has been created! This ActiveX DLL component written with asp built-in objects has been destroyed!
Asp component intermediate entry and mastery series Part 2
Let's first look at the Application object. Previously, the Application object was often used for counters and databases. For the connection string, we take the counter as an example:
Let’s look at the global.asa file first. This is relatively simple.
program code
<script language =vbscript runat=server>
subApplication_onstart
Application("Counter")=0
end sub
</script>
Then
open vb6 and create a new Activex Dll project. The project name is changed to fCom and the class name is changed to fZ2
Reference to the "Microsoft Active Server Pages Object" object library.
Create two component events: OnStartPage and OnEndPage
Create a reference to class ScriptingContent in the event OnStartPage.
Instantiate class ScriptingContent.
The code is as follows:
program code
Option Explicit
'Object declaration
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'This event will be triggered when the component is created
Public Sub OnStartPage(myScriptingContent As ScriptingContext)
'Execute object instantiation
Set myResponse = myScriptingContent.Response
Set myRequest = myScriptingContent.Request
Set myServer = myScriptingContent.Server
Set myApplication = myScriptingContent.Application
Set mySession = myScriptingContent.Session
End Sub
'This event is triggered when the component is destroyed
Public Sub OnEndPage()
'Destroy the object
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'You can see that the writing method is the same after moving what was written in asp to 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
Test
open visual interdev6.0 and generate an asp file
program code
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim obj
set obj=server.CreateObject("fCom.fZ2")
obj.ShowCounter()
%>
</BODY>
</HTML>
After configuring the virtual directory, you need to put the global.asa file in the root directory, execute this asp file in IE, refresh the page, and you will see a changing number.
That’s it for the usage of Application.
the intermediate entry level of Asp component and the mastery series three,
Session, it is much simpler
. Open vb6 and create a new Activex Dll project. Change the project name to fCom and the class name to fZ3
Reference to the "Microsoft Active Server Pages Object" object library.
Create two component events: OnStartPage and OnEndPage
Create a reference to class ScriptingContent in the event OnStartPage.
Instantiate class ScriptingContent.
The code is as follows:
program code
Option Explicit
'Object declaration
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'This event will be triggered when the component is created
Public Sub OnStartPage(myScriptingContent As ScriptingContext)
'Execute object instantiation
Set myResponse = myScriptingContent.Response
Set myRequest = myScriptingContent.Request
Set myServer = myScriptingContent.Server
Set myApplication = myScriptingContent.Application
Set mySession = myScriptingContent.Session
End Sub
'This event is triggered when the component is destroyed
Public Sub OnEndPage()
'Destroy the object
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'You can see that the writing method is the same after moving what was written in asp to vb.
'Get all session variables and values
Public Sub ShowSession()
'You can set a timeout of 20 minutes
mySession.Timeout = 20
Dim myitem
'Get all sessions
For Each myitem In mySession.Contents
myResponse.Write myitem & ": " & mySession.Contents(myitem)
myResponse.Write "<br>"
Next
End Sub
Test
open visual interdev6.0, generate an asp file to configure the virtual directory, execute this asp file in IE, you can see
the name: tornado
age: 26
Specialty:
That’s it for the usage of component Session. Other uses of Session are similar.
Asp component intermediate entry and mastery series four,
we learn to Request
To see how to get the content submitted by the page in the component
, open vb6 and create a new Activex Dll project. The project name is changed to fCom and the class name is changed to fZ4
Reference to the "Microsoft Active Server Pages Object" object library.
Create two component events: OnStartPage and OnEndPage
Create a reference to class ScriptingContent in the event OnStartPage.
Instantiate class ScriptingContent.
The code is as follows:
program code
Option Explicit
'Object declaration
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'This event will be triggered when the component is created
Public Sub OnStartPage(myScriptingContent As ScriptingContext)
'Execute object instantiation
Set myResponse = myScriptingContent.Response
Set myRequest = myScriptingContent.Request
Set myServer = myScriptingContent.Server
Set myApplication = myScriptingContent.Application
Set mySession = myScriptingContent.Session
End Sub
'This event is triggered when the component is destroyed
Public Sub OnEndPage()
'Destroy the object
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'You can see that the writing method is the same after moving what was written in asp to vb.
Public Sub ShowRequest()
Dim myitem
'Post way
For Each myitem In myRequest.Form
myResponse.Write myitem & ": " & myRequest.Form(myitem)
myResponse.Write "<br>"
Next
'Get method
For Each myitem In myRequest.QueryString
myResponse.Write myitem & ": " & myRequest.QueryString(myitem)
myResponse.Write "<br>"
Next
'Single message
myResponse.Write "One of the messages is " & ": " & myRequest("username")
myResponse.Write "<br>"
End Sub
Test
open visual interdev6.0 and generate a fz41.asp file
program code
<%@ 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=submit>
</form>
</BODY>
</HTML>
You also need to generate a submitted fz4_result.asp file
program code
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim obj
set obj=server.CreateObject("fCom.fZ4")
call obj.ShowRequest
%>
</BODY>
</HTML>
In addition, we also need to take a look at the submission method, so we need a fz42.asp file
program code
<%@ 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=submit>
</form>
</BODY>
</HTML>
Configure the virtual directory and execute the fc41.asp file in IE. After entering the content, click the button and you will see
the username: Tornado
age: 26
Submit: Submit
One of the messages is: Tornado.
Let's execute the fc42.asp file in IE. After entering the content, click the button and you can see
the username: Tornado.
age: 26
Submit: Submit
One of the messages is: Tornado
and the address bar becomes
http://yang/xml/fz4_result.asp?username=%C1%FA%BE%ED%B7%E7&age=26&%CC%E1%BD%BB=Submit
To be continued
Asp component intermediate entry and mastery series Part 5
Let's take a look at the Response object. In fact, we have been using the Write method of this object in previous tutorials.
Here we use the Response object to set cookies.
Open vb6 and create a new Activex Dll project. Change the project name to fCom and the class name to fZ5
Reference to the "Microsoft Active Server Pages Object" object library.
Create two component events: OnStartPage and OnEndPage
Create a reference to class ScriptingContent in the event OnStartPage.
Instantiate class ScriptingContent.
The code is as follows:
program code
Option Explicit
'Object declaration
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'This event will be triggered when the component is created
Public Sub OnStartPage(myScriptingContent As ScriptingContext)
'Execute object instantiation
Set myResponse = myScriptingContent.Response
Set myRequest = myScriptingContent.Request
Set myServer = myScriptingContent.Server
Set myApplication = myScriptingContent.Application
Set mySession = myScriptingContent.Session
End Sub
'This event is triggered when the component is destroyed
Public Sub OnEndPage()
'Destroy the object
Set myResponse = Nothing
Set myRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'Set cookies from the page and get them from the component
Public Sub GetCookie()
Dim myitem
'All information
For Each myitem In myRequest.Cookies
myResponse.Write myitem & ": " & myRequest.Cookies.Item(myitem)
myResponse.Write "<br>"
Next
'Single message
myResponse.Write "where the user name is " & ": " & myRequest.Cookies("username")
myResponse.Write "<br>"
myResponse.Write "where the user's age is" & ": " & myRequest.Cookies("age")
myResponse.Write "<br>"
End Sub
'Set cookies in the component and get them in the page
Public Sub SetCookie()
myResponse.Cookies("com_username") = "Tornado"
myResponse.Cookies("com_age") = 26
myResponse.Expires = #9/13/2004#
End Sub
Compile it into a Dll file and the system will automatically register it.
Otherwise, register Regsvr32 manually f:testfcom.dll
to test
open visual interdev6.0, generate a fz5.asp file
and configure the virtual directory, execute the fc5.asp file in IE, you can see
the tornado
26
age: 26
username: tornado
com_age: 26
com_username: Tornado where the user name is: Tornado where the user age is: 26
To be continued
Intermediate Introduction and Mastery of Asp Components Series No. 6
As a built-in object of Asp, we finally learn about the Server object
. The Server object uses more Html coding. URL encoding and web page redirection and transmission.
Open vb6 and create a new Activex Dll project. The project name is changed to fCom and the class name is changed to fZ6
Reference to the "Microsoft Active Server Pages Object" object library.
Create two component events: OnStartPage and OnEndPage
Create a reference to class ScriptingContent in the event OnStartPage.
Instantiate class ScriptingContent.
The code is as follows:
program code
Option Explicit
'Object declaration
Dim myResponse As Response
Dim myRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'This event will be triggered when the component is created
Public Sub OnStartPage(myScriptingContent As ScriptingContext)
'Execute object instantiation
Set myResponse = myScriptingContent.Response
Set myRequest = myScriptingContent.Request
Set myServer = myScriptingContent.Server
Set myApplication = myScriptingContent.Application
Set mySession = myScriptingContent.Session
End Sub
'This event is triggered when the component is destroyed
Public Sub OnEndPage()
'Destroy the object
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
Compile it into a Dll file and the system will automatically register it.
Otherwise, manually register Regsvr32 f:testfcom.dll
to test
open visual interdev6.0, generate a fz6.asp file
and configure the virtual directory. Execute the fc6.asp file in IE to see it
. Test
name=Mrs+%C1 %FA%BE%ED%B7%E7&age=26
You can use IE to view the source file to view HTML encoding.
This is the temporary introduction of ASP's built-in objects, and we will continue to study them later.
You can also draw inferences from one example and learn properties and methods that have not been introduced.
Asp component intermediate entry and mastery series seven
starts database operations.
Common component packaging
1. Encapsulate the database connection information.
1> Directly return the database connection string, such as
the program code
in the component
Public Function datasource() As Variant
datasource = "driver={sql server};server=yang;uid=sa;pwd=; database=northwind"
End Function
asp calling
program code
set obj=server.CreateObject("webdb.getinfo")
oconn=obj.datasource()
are obvious. In the asp file, directly response.write oconn can display the database connection string, which does not have the expected effect.
2> Return adodb.connection object
program code
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 calling
program code
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
looks good, but Response.write DataQuery.getconn.ConnectionString will still display the database connection string. You can test it.
2. To encapsulate components into a record set, you can read what I wrote some time ago : http://blog.csdn.net/online/archive/2003/12/11/7764.aspx
The bad thing about this code is that the database connection is judged on the page. After the connection is successful, the data is accessed. Personally, I think the best way is to
encapsulate it into a record set and connect to the database in the component method. After the operation is completed, Close it in
time and try to generate HTML code in the component to encapsulate everything. Such as the following method
instead of partial encapsulation.
Asp component intermediate entry and mastery series No. 8
has been quite busy during this period. Haha, today we will take a look at a complete data encapsulation and paging example.
Open vb6 and create a new Activex Dll project. The project name is changed to fCom and the class name is changed to fZ8
Reference to "Microsoft Active Server Pages Object", "Microsoft Activex Data Object 2.7 Library" object library.
Create two component events: OnStartPage and OnEndPage
Create a reference to class ScriptingContent in the event OnStartPage.
Instantiate class ScriptingContent.
The code is as follows:
program code
Option Explicit
'************************************************ *
'Author: Tornado
'Function: Simple customizable, fully encapsulated component
'Time:2005-01-01
'************************************************ *
'Object declaration
Dim MyResponse As Response
Dim MyRequest As Request
Dim myApplication As Application
Dim myServer As Server
Dim mySession As Session
'Private variables
Private mPageSize As Long
Private mstrSql As String
'This event will be triggered when the component is created
Public Sub OnStartPage(myScriptingContent As ScriptingContext)
'Execute object instantiation
Set MyResponse = myScriptingContent.Response
Set MyRequest = myScriptingContent.Request
Set myServer = myScriptingContent.Server
Set myApplication = myScriptingContent.Application
Set mySession = myScriptingContent.Session
End Sub
'This event is triggered when the component is destroyed
Public Sub OnEndPage()
'Destroy the object
Set MyResponse = Nothing
Set MyRequest = Nothing
Set myServer = Nothing
Set myApplication = Nothing
Set mySession = Nothing
End Sub
'Show 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
'Get the path
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
'Get the number of records
intFieldCount = rs.Fields.Count
'Output table
MyResponse.Write "<table border=1 cellspacing=0 cellpadding=2>"
If Not rs.EOF Then
rs.PageSize = mPageSize
rs.AbsolutePage = intPage
'Get the page number
intPageCount = rs.PageCount
'Handle paging
If intPage < 1 Then intPage = 1
If intPage > intPageCount Then intPage = intPageCount
'Output header
MyResponse.Write "<tr>"
For i = 0 To intFieldCount - 1
MyResponse.Write "<th>" & rs(i).Name & "</th>"
Next
MyResponse.Write "</tr>"
'Output content
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
'Output paging
MyResponse.Write "<tr>"
If intPage <> 1 Then
MyResponse.Write "<a href=" & strScriptName & "?page=1>[First page]</a>"
MyResponse.Write "<a href=" & strScriptName & "?page=" & intPage - 1 & " >[Previous Page]</a>"
End If
If intPage <> intPageCount Then
MyResponse.Write "<a href=" & strScriptName & "?page=" & intPage + 1 & ">[next page]</a>"
MyResponse.Write "<a href=" & strScriptName & "?page=" & intPageCount & ">[last page]</a>"
End If
MyResponse.Write "Page:<FONT COLOR='Red'>" & intPage & "/ " & intPageCount & "</FONT>"
MyResponse.Write "</tr>"
End If
MyResponse.Write "</table>"
'Release resources
If Not rs Is Nothing Then
If rs.State = 1 Then
rs.Close
End If
Setrs=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
Setrs=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
'Define attributes
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
Compile it into a Dll file and the system will automatically register it.
Otherwise, manually register Regsvr32 f:testfcom.dll
to test
open visual interdev6.0 and generate a fz8.asp file
program code
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim obj
set obj=server.CreateObject("fcom.fz8")
'The number of records displayed on each page
obj.ShowPageSize=10
'Displayed sql statement
obj.strSQL="select customerid,companyname,contactname,contacttitle,address from customers"
obj.ShowTable()
%>
</BODY>
</HTML>