661 lines
42 KiB
HTML
661 lines
42 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||
<HTML dir=ltr><HEAD><TITLE>Module 2: Using COM Components</TITLE>
|
||
<SCRIPT language=JavaScript>
|
||
<!--
|
||
TempString = navigator.appVersion
|
||
if (navigator.appName == "Microsoft Internet Explorer"){
|
||
// Check to see if browser is Microsoft
|
||
if (TempString.indexOf ("4.") >= 0){
|
||
// Check to see if it is IE 4
|
||
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
|
||
}
|
||
else {
|
||
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
|
||
}
|
||
}
|
||
else if (navigator.appName == "Netscape") {
|
||
// Check to see if browser is Netscape
|
||
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
|
||
}
|
||
else
|
||
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
|
||
//-->
|
||
</SCRIPT>
|
||
|
||
<SCRIPT language=VBScript>
|
||
<!--
|
||
Sub Window_OnLoad()
|
||
Dim frmContents
|
||
On Error Resume Next
|
||
If Not Parent Is Nothing Then
|
||
Set frmContents = Parent.Contents
|
||
If Not frmContents Is Nothing Then
|
||
frmContents.Window.TOCSynch_Click
|
||
End If
|
||
End If
|
||
End Sub
|
||
//-->
|
||
</SCRIPT>
|
||
|
||
<META
|
||
content="This is an Active Server Pages (ASP) tutorial where you can lean how to use COM components to extend your scripting capabilities. In this module, you use COM components that are included with ASP to activate a sample Web site and query a database."
|
||
name=DESCRIPTION>
|
||
<META content="text/html; charset=Windows-1252" http-equiv=Content-Type>
|
||
<META content="Microsoft FrontPage 4.0" name=GENERATOR></HEAD>
|
||
<BODY bgColor=#ffffff text=#000000><FONT face=Verdana,Arial,Helvetica>
|
||
<H1><A name=module1>Module 2: Using COM Components in ASP Pages</A></H1>
|
||
<P>Using Microsoft® Component Object Model (COM) components in your ASP pages can
|
||
dramatically extend the power that ASP already offers. COM components are pieces of compiled code that can
|
||
be called from ASP pages. COM components are secure,
|
||
compact, reusable objects that are compiled as DLLs and can be written in
|
||
Microsoft Visual C++®, Microsoft Visual Basic®, or any language that supports COM. For example, Microsoft
|
||
ActiveX® Data Objects (ADO), which are used in Module 1 of this tutorial, provides you with methods and
|
||
properties to efficiently query databases. By using ADO, you don't have to write your own
|
||
complex data access code because ADO objects are built using COM components. </P>
|
||
<P>In this module, you use COM components that are included with ASP, and you also have a chance to write one
|
||
of your own.
|
||
This module shows how to develop an ASP page that delivers services useful
|
||
in e-commerce and includes the following lessons: </P>
|
||
<UL>
|
||
<LI><A
|
||
href="#rotating">Lesson
|
||
1: Rotating Advertisements</A> Rotate ads on a Web page, record user data, and
|
||
redirect users when advertisements are clicked on.
|
||
<LI><A
|
||
href="#counting">Lesson
|
||
2: Counting Page Hits</A> Track the number of times users request a page.
|
||
<LI><A
|
||
href="#creating">Lesson
|
||
3: Creating a Visual Basic COM Object</A> Create your own ActiveX object using
|
||
Microsoft Visual Basic.
|
||
<LI><A
|
||
href="#creatingj">Lesson
|
||
4: Creating a Java COM Object</A> Create your own Java object using Microsoft
|
||
Visual J++®. </LI></UL><BR>
|
||
<H2><A name=rotating>Lesson 1: Rotating Advertisements</A></H2>
|
||
<P>Advertising is big business on the Web. This lesson describes how to use the
|
||
Ad Rotator component, installed with IIS, to rotate advertisements on your Web
|
||
pages. The Ad Rotator component selects an advertisement for your Web page each
|
||
time the user loads or refreshes the Web page. Furthermore, if you want to
|
||
change an advertisement, you only need to change it in the redirection and
|
||
rotation schedule files, instead of changing all the ASP files that contain the
|
||
advertisement. This saves development time if the advertisement appears on
|
||
numerous pages within your Web site. </P>
|
||
<P>Two files are required to set up the Ad Rotator component: a redirection
|
||
file, which contains URL links to ads, and a rotation schedule file, which
|
||
contains display data. By setting up these two files, the Ad Rotator component
|
||
can be called by any ASP page on your Web site. </P>
|
||
<P>In this lesson you perform the following tasks: </P>
|
||
<UL>
|
||
<LI><b>Example 1</b> Create an Ad Rotator rotation schedule file that creates ad-image links on
|
||
any page that calls this file.
|
||
<LI><b>Example 2</b> Create an Ad Rotator redirection file that specifies global ad-display
|
||
data and information specific to each advertisement.
|
||
<LI><b>Example 3</b> Create an include file to hold your Ad
|
||
Rotator calling code.
|
||
<LI><b>Example 4</b> Test the Ad Rotator by creating an ASP page that calls the Ad Rotator
|
||
component to display and rotate ads. This example requires that examples 1, 2,
|
||
and 3 are completed first. </LI></UL>
|
||
<P>
|
||
<H3>Example 1: Create an Ad Rotator Rotation Schedule File</H3>
|
||
<P>A rotation schedule file is used to catalog information about the ads you
|
||
want to display, including redirection information after the advertisement is
|
||
clicked on, the size of the displayed advertisement, the image to display, a
|
||
comment for the advertisement, and a number that indicates how often a
|
||
particular advertisement is chosen. When methods of the Ad Rotator component are
|
||
called in an ASP page, the component uses this file to select an advertisement
|
||
to display. </P>
|
||
<P>The rotation schedule file is divided into two sections that are separated by
|
||
an asterisk (*). The first section provides information common to all the ads,
|
||
and the second section lists specific data for each ad. To test the rotation
|
||
schedule file, you will use some images from Microsoft.com for your ad images.
|
||
The following list outlines the structure of the rotation schedule file: </P>
|
||
<h5>Section 1</h5>
|
||
<UL>
|
||
<LI><B>Redirection</B> In URL form, the path and name of the ASP file that
|
||
can be executed before showing an advertisement. This file can be used to record
|
||
information about the user who clicks on your ad. You can record information such as the client's IP address, what page the client
|
||
saw the advertisement on, how often an advertisement was clicked on, and so forth. This ASP
|
||
file can also handle the case where there is no URL associated with any
|
||
advertisement in Section 2. When charging advertisers for each hit on their
|
||
advertisement, it is good practice to prove to them that all the hits aren't resulting from the same user repeatedly hitting <B>Refresh</B>.
|
||
<LI><B>Width</B> The width of each ad image, in pixels. The default is 440.
|
||
<LI><B>Height</B> The height of each ad image, in pixels. The default is 60.
|
||
<LI><B>Border</B> The border thickness around each ad image. The default is 1.
|
||
<LI><B>Asterisk (*)</B> Separates the first section from the second section. This character must be on
|
||
a line by itself. </LI></UL>
|
||
<h5>Section 2</h5>
|
||
<P>You need to complete the following for each advertisement:
|
||
<UL>
|
||
<LI><B>Image URL</B> The virtual path and filename of the image file for the
|
||
advertisement.
|
||
<LI><B>Advertiser's Home URL</B> The URL to jump to when this link is
|
||
selected. If there is no link, use a hyphen (-).
|
||
<LI><B>Text</B> The text to display if the browser does not support graphics.
|
||
|
||
<LI><B>Impressions</B> An integer indicating the relative weight to give to
|
||
this ad when the Ad Rotator component selects an advertisement. For example,
|
||
if you list two advertisements, an ad given an impression of 3 has a 30%
|
||
probability of being selected while an ad given an impression of 7 has a 70%
|
||
probability of being selected. In this example, the Ad Rotator component
|
||
selects the Microsoft Windows® advertisement two times out of five and the
|
||
Microsoft Office advertisement is selected three times out of five. </LI></UL>
|
||
<P>Copy and paste the following code in your text editor, and save the file as
|
||
<B>MyAdRot.txt</B> in the <I>x</I>:\Inetpub\Wwwroot\Tutorial directory.
|
||
</P><CODE>
|
||
<P> REDIRECT AdRotRedirect.asp <BR> WIDTH 250 <BR> HEIGHT 60
|
||
<BR> BORDER 0 <BR> * <BR> http://www.microsoft.com/windows/images/bnrWinfam.gif <BR>
|
||
http://www.microsoft.com/windows <BR> Microsoft Windows <BR> 2
|
||
<BR> http://www.microsoft.com/office/images/office_logo.gif <BR>
|
||
http://www.microsoft.com/office <BR> Office 2000 <BR> 3 </P>
|
||
<P> </P></CODE>
|
||
<h3>Example 2: Create an Ad Rotator Redirection File
|
||
</h3>
|
||
<P> When a user clicks on an advertisement, an Ad Rotator redirection file
|
||
written in ASP can capture some
|
||
information before showing the advertisement and write that information to a
|
||
file.
|
||
</P>
|
||
<P> For this to work, the <I>x</I>:\InetPub\Wwwroot\Tutorial folder must give
|
||
Read/Write access to the IUSR_<I>ComputerName</I> and IWAM_<I>ComputerName</I>
|
||
accounts. Alternatively, you can write this information to a Microsoft Access database
|
||
using the code from <A href="ASPTut01.htm#creating">Lesson 3 in Module 1</a> of this tutorial.
|
||
</P>
|
||
<P>Copy and paste the following code in your text editor, and save the file as
|
||
<B>AdRotRedirect.asp</B> in the <I>x</I>:\Inetpub\Wwwroot\Tutorial directory.
|
||
</P><CODE>
|
||
<P> <%@ Language=VBScript %> <BR><BR> <html> <BR>
|
||
<head> <BR> <title>AdRotRedirect file</title> <BR>
|
||
</head> <BR> <body> <BR><BR> <% <BR>
|
||
'Create some variables. <BR> dim strLogFile <BR><BR>
|
||
'Get the physical path of this Web directory so that we know the path exists.
|
||
<BR> 'The ASP Server object has many useful methods. <BR> strLogFile = Server.MapPath(".") &
|
||
"\AdRotLog.txt" <BR><BR> 'Set some constants for working with files.
|
||
<BR> Const cForAppending = 8 <BR> Const
|
||
cTristateUseDefault = -2 <BR><BR> 'Create a FileSystemObject object,
|
||
<BR> ' which gives you access to files and folders on the system.
|
||
<BR> Set fsoObject =
|
||
Server.CreateObject("Scripting.FileSystemObject") <BR><BR> 'Open a
|
||
handle to the file. <BR> 'True means that the file will be created
|
||
if it doesn't already exist. <BR> Set tsObject =
|
||
fsoObject.OpenTextFile(strLogFile, cForAppending, True) <BR><BR>
|
||
'Record the data for the user who has just clicked on an advertisement.
|
||
<BR> 'We have used the Write method of the ASP Request object.
|
||
<BR> 'The ServerVariables collection of the ASP Request object holds vast <BR> ' amounts of data for
|
||
each request made to a Web server. <BR>
|
||
tsObject.WriteLine "--------------------" <BR> tsObject.WriteLine
|
||
Date & ", " & Time <BR> tsObject.WriteLine
|
||
Request.ServerVariables("LOGON_USER") <BR> tsObject.WriteLine
|
||
Request.ServerVariables("REMOTE_ADDR") <BR> tsObject.WriteLine
|
||
Request.QueryString("url") <BR> tsObject.WriteLine
|
||
Request.ServerVariables("HTTP_REFERER") <BR> tsObject.WriteLine
|
||
Request.ServerVariables("HTTP_USER_AGENT") <BR> tsObject.Close
|
||
<BR><BR> 'Redirect to the Advertiser's Web site using the Redirect
|
||
method <BR> ' of the ASP Response object. <BR> 'When the
|
||
AdRotator component calls AdRotRedirect.asp, it <BR> '
|
||
automatically passes in the advertiser's URL in the QueryString.
|
||
<BR> Response.Redirect Request.QueryString("url") <BR> %>
|
||
<BR><BR> </body> <BR> </html> </P></CODE>
|
||
<P>
|
||
</P>
|
||
<h3>Example 3: Create an Ad Rotator Include File
|
||
</h3>
|
||
<P>Include files are used to store any code that will be used by more than one
|
||
ASP or HTML file. It makes sense to put your Ad Rotator code into a simple
|
||
function in an include file. With an Ad Rotator include file, you need to
|
||
make only one function call from any ASP or HTML file when you want to display an
|
||
advertisement. Alternatively, you can put the code from the include file in every ASP file where you
|
||
plan to show an advertisement. However, if you want to change that code, you
|
||
have to make the change in every ASP file instead of in one include file.
|
||
</P>
|
||
<P>In this example, you create an Ad Rotator include file containing a function
|
||
named <b>GetAd</b>. This function randomly selects ads to display on your ASP pages.
|
||
</P>
|
||
<P>Copy and paste the following code in your text editor, and save the file as
|
||
<B>AdRotatorLogic.inc</B> in the <I>x</I>:\Inetpub\Wwwroot\Tutorial directory.
|
||
</P><CODE>
|
||
<P> <% <BR> Function GetAd()
|
||
<BR><BR> dim objLoad <BR><BR>
|
||
'Create an instance of the AdRotator component. <BR> Set
|
||
objLoad = Server.CreateObject("MSWC.AdRotator") <BR><BR>
|
||
'Set the TargetFrame property, if any. If you have a Web
|
||
<BR> ' page using frames, this is the frame where the
|
||
URL opens. <BR> 'If the HTML page does not find the
|
||
TARGET name, <BR> ' the URL will be opened in a new
|
||
window. <BR> objLoad.TargetFrame = "TARGET=new"
|
||
<BR><BR> 'Set one of the other AdRotator properties.
|
||
<BR> objLoad.Border = 1 <BR><BR>
|
||
'Get a random advertisement from the text file. <BR>
|
||
GetAd = objLoad.GetAdvertisement("MyAdRot.txt") <BR><BR> End
|
||
Function <BR> %> </P>
|
||
<P> </P></CODE>
|
||
<H3>Example 4: Test the Ad Rotator</H3>
|
||
<P>To test the application you have built on the Ad Rotator component, you need
|
||
an ASP page that calls the function in the Ad Rotator include file you created.
|
||
</P>
|
||
<P>Copy and paste the following code in your text editor, and save the file as
|
||
<B>DisplayAds.asp</B> in the <I>x</I>:\Inetpub\Wwwroot\Tutorial directory. View
|
||
the example with your browser by typing
|
||
<B>http://localhost/Tutorial/DisplayAds.asp</B> in the address bar. </P><CODE>
|
||
<P> <%@ Language=VBScript %> <BR><BR> <html> <BR>
|
||
<head> <BR> <title>Display an Advertisement</title>
|
||
<BR> </head> <BR> <body> <BR><BR> <font
|
||
face="MS Gothic"> <BR> <h2>Display an Advertisement</h2>
|
||
<BR><BR> <comment>Include the file you created to get an
|
||
advertisement.</comment> <BR> <!-- #include File =
|
||
"AdRotatorLogic.inc" --> <BR><BR> <comment>Call the Function in
|
||
the include file.</comment> <BR> <%=GetAd()%> <BR><BR>
|
||
</font> <BR> </body> <BR> </html> </P></CODE>
|
||
<P>In the browser, you should see the following: </P>
|
||
<P><FONT face="MS Gothic">
|
||
<H2>Display an Advertisement</H2>
|
||
<IMG SRC="http://www.microsoft.com/windows/images/bnrWinfam.gif" ALT="Microsoft Windows " WIDTH=250 HEIGHT=60 BORDER=1></FONT>
|
||
<P>Click the <B>Refresh</B> button in your browser about 20 times to watch the
|
||
advertisement change. Click the advertisement to see how AdRotRedirect.asp
|
||
redirects you to the advertiser's Web site. Open AdRotLog.txt to see what was
|
||
recorded when you clicked on an advertisement. </P>
|
||
<P> </P>
|
||
<H2><A name=counting>Lesson 2: Counting Page Hits</A></H2>
|
||
<P>It may be useful to know how many times someone requests, or <i>hits</i>, your Web
|
||
pages. Sites with high traffic may attract more advertising revenue for you.
|
||
Some Web sites use this data to charge advertisers a flat fee per hit. Traffic
|
||
information also indicates how customers are navigating through your site and
|
||
where ads should be placed. And pages that never seem to be hit might indicate that design changes are needed. </P>
|
||
<P>The PageCounter component uses an internal object to record Web page hits on
|
||
the server. At regular intervals, PageCounter saves all information to a text
|
||
file so that no counts are lost due to power loss or system failure. The
|
||
PageCounter component uses three methods, as follows: </P>
|
||
<UL>
|
||
<LI><B>Hits</B> This method displays the number of hits for a Web page. The
|
||
default is the calling page.
|
||
<LI><B>PageHit</B> This method increments the hit count for the current page. If
|
||
you want hits recorded for an ASP page, this method must be called inside that
|
||
page.
|
||
<LI><B>Reset</B> This method sets the hit count for a page to zero. The default is
|
||
the calling page. </LI></UL>
|
||
<P>Copy and paste the following code in your text editor, and save the file as
|
||
<B>PageCounter.asp</B> in the <I>x</I>:\Inetpub\Wwwroot\Tutorial directory. View
|
||
the example with your browser by typing
|
||
<B>http://localhost/Tutorial/PageCounter.asp</B> in the address bar. </P><CODE>
|
||
<P> <%@ Language=VBScript %> <BR><BR> <html> <BR>
|
||
<head> <BR> <title>Page Counter Example</title>
|
||
<BR> </head> <BR> <body> <BR> <font face="MS
|
||
Gothic"> <BR><BR> <H3>Page Counter Example</H3>
|
||
<BR><BR> <p> <BR> <FORM NAME="PageCounter" METHOD="GET"
|
||
ACTION="PageCounter.asp"> <BR> <INPUT TYPE="CHECKBOX" NAME="reset"
|
||
VALUE="True">Reset the Counter for this page?<BR> <BR> <INPUT
|
||
TYPE="SUBMIT" VALUE="Submit"> <BR> </FORM> <BR> </p>
|
||
<BR><BR> <% <BR> 'Instantiate the PageCounter object.
|
||
<BR> Set MyPageCounter = Server.CreateObject("MSWC.PageCounter")
|
||
<BR><BR> 'Increment the counter for this page. <BR>
|
||
MyPageCounter.PageHit <BR><BR> If Request.QueryString("reset") =
|
||
"True" Then <BR> 'Reset the counter for this page.
|
||
<BR> MyPageCounter.Reset("/Tutorial/PageCounter.asp")
|
||
<BR> End If <BR> %> <BR><BR> Hits to this page =
|
||
<%=MyPageCounter.Hits %><BR> <BR><BR> </font> <BR>
|
||
</body> <BR> </html> </P></CODE>
|
||
<P>In the browser, you should see the following: </P>
|
||
<P><FONT face="MS Gothic">
|
||
<H3> Page Counter Example</H3>
|
||
<P>
|
||
<FORM method=get name=PageCounter> <INPUT name=reset type=checkbox
|
||
value=True>Reset the Counter for this page?<BR> <INPUT type=button value=Submit> </FORM>
|
||
<P> Hits to this page = 1<BR></FONT>
|
||
<P>Click the <B>Refresh</B> button in your browser or the <B>Submit</B> button
|
||
on the page to watch the hit count grow. Check the box if you want to reset the
|
||
counter. </P><BR>
|
||
<H2><A name=creating>Lesson 3: Creating a Visual Basic COM Object</A></H2>
|
||
<P>In this lesson, you use Visual Basic to create a simple COM object, which you
|
||
can call from an ASP page. This example requires Visual Basic with the ActiveX
|
||
Wizard, and it is not supported on 64-bit platforms until the Visual Basic runtime is
|
||
developed for 64-bit platforms. You create a 32-bit COM object that runs on a
|
||
64-bit platform, but you must call the 32-bit COM object from a 32-bit
|
||
application. Because IIS is a 64-bit application on 64-bit platforms, it cannot
|
||
call 32-bit objects. </P>
|
||
<P>Suppose you want to create a Web application that needs functionality
|
||
VBScript does not have. In this case, you must create a custom procedure that is
|
||
called, as needed, from any ASP page in your application.</P>
|
||
<P>Normally, this approach is an adequate solution for encapsulating custom
|
||
functionality. However, imagine that you are creating a Web application intended
|
||
to service thousands of users and that your procedure encapsulates proprietary
|
||
functions you do not want other people to see. In this case, encapsulating your
|
||
functionality in the form of a COM component is a superior approach. Components
|
||
provide better security and performance than scripts because they are compiled
|
||
code. Components also allow you to use functionality provided by Visual Basic,
|
||
C++, Java, or any of the other COM-compliant languages.</P>
|
||
<H3>Create the ActiveX COM Object</H3>
|
||
<P>The ActiveX DLL Wizard of Visual Basic is the easiest way to create a COM
|
||
component. You can also use Microsoft Visual C++ to create a COM component,
|
||
either by using the Active Template Library (ATL) or by writing all the code
|
||
yourself. This example uses Visual Basic. </P>
|
||
<P>In this lesson, you learn how to create and encapsulate a
|
||
Visual Basic function as a component. Visual Basic includes many financial
|
||
functions not available to VBScript. This example computes the future value of
|
||
an investment based on a fixed interest rate and periodic, fixed payments.</P>
|
||
<OL>
|
||
<LI>Open Visual Basic. If you don't see a window titled <B>New Project</B>,
|
||
choose <B>File</B> and then click <B>New Project</B>.
|
||
<LI>Select <B>ActiveX DLL</B>, and click <B>OK</B>.
|
||
<LI>A window should open called <B>Project1 - Class1 (Code)</B>. This is where
|
||
you enter your code.
|
||
<LI>From the <STRONG>Project </STRONG>menu, click <STRONG>Project1
|
||
Properties</STRONG>. In the <STRONG>General</STRONG> property sheet, in the
|
||
<STRONG>Project Name</STRONG> box, type <B>ASPTut</B>. Your DLL is called
|
||
ASPTut.dll. Select the <STRONG>Unattended Execution</STRONG> check box so that the
|
||
project runs without user interaction and has no user interface elements. Make
|
||
sure the <B>Threading Model</B> is <B>Apartment Threaded</B> so that more than
|
||
one user at a time can use the DLL. Click <STRONG>OK</STRONG>.
|
||
<LI>In Visual Basic, you define a class to group together methods and
|
||
properties. Under the <B>Project - ASPTut</B> window, click the <B>Class1
|
||
(Class1)</B> node to list the class properties below. Under <B>Properties -
|
||
Class1</B>, click in the text field beside <B>(Name)</B> and change the class
|
||
name to <B>Finance</B>. When you call this COM component in an ASP page or
|
||
other script, you will reference it with <b>ASPTut.Finance</b>. Click the drop-down box beside <B>Instancing</B>, and select <B>5 - MultiUse</B>.
|
||
<LI>Learn about the Visual Basic function you are about to use. <b>FV</b> is
|
||
documented on <a href="http://go.microsoft.com/fwlink/?LinkId=1694" target="_blank">MSDN</A>
|
||
under the Visual Basic library.
|
||
<LI>The window that was previously titled <B>Project1 - Class1 (Code)</B> should
|
||
now be titled <B>ASPTut - Finance (Code)</B>. Copy and paste the following
|
||
text into that window:<CODE> <BR><BR>Option Explicit<BR><BR>'Declare the
|
||
global variables that will be set by the Property functions.<BR>Dim
|
||
gAnnualIntRate As Double<BR>Dim gNumPayPeriods As Integer<BR>Dim gPayment As
|
||
Double<BR>Dim gPresentSavings As Variant 'Optional<BR>Dim gWhenDue As Variant
|
||
'Optional<BR><BR>Public Function CalcFutureValue() As Double<BR><BR>
|
||
'The global variables you pass to the FV function are set<BR> 'when user
|
||
sets the properties in the ASP page.<BR> 'You could pass variables into
|
||
the CalcFutureValue() function<BR> 'if you wanted to avoid using
|
||
properties.<BR> 'CalcFutureValue becomes a method in your
|
||
component.<BR><BR> Dim IntRatePerPeriod As Double<BR> Dim
|
||
FullFutureValue As Double<BR><BR> If (gAnnualIntRate = Null) Or
|
||
(gNumPayPeriods = Null) Or (gPayment = Null) Then<BR>
|
||
CalcFutureValue = 0<BR> Else<BR> IntRatePerPeriod =
|
||
gAnnualIntRate / 100 / 12<BR> FullFutureValue =
|
||
FV(IntRatePerPeriod, gNumPayPeriods, gPayment, gPresentSavings,
|
||
gWhenDue)<BR> CalcFutureValue = Round(FullFutureValue,
|
||
2)<BR> End If<BR><BR>End Function<BR><BR>Public Property Get
|
||
AnnualIntRate() As Double<BR> 'Get functions return the value of the
|
||
global variables<BR> 'as if they were properties.<BR> 'In your
|
||
ASP page, you might say x = oASPTut.Rate.<BR> AnnualIntRate =
|
||
gAnnualIntRate<BR>End Property<BR><BR>Public Property Let AnnualIntRate(ByVal
|
||
vAnnualIntRate As Double)<BR> 'Let functions set the global variables
|
||
when your ASP page<BR> 'makes a call such as oASPTut.Rate = 5.<BR>
|
||
gAnnualIntRate = vAnnualIntRate<BR>End Property<BR><BR>Public Property Get
|
||
NumPayPeriods() As Integer<BR> NumPayPeriods = gNumPayPeriods<BR>End
|
||
Property<BR><BR>Public Property Let NumPayPeriods(ByVal vNumPayPeriods As
|
||
Integer)<BR> gNumPayPeriods = vNumPayPeriods<BR>End
|
||
Property<BR><BR>Public Property Get Payment() As Double<BR> Payment =
|
||
gPayment<BR>End Property<BR><BR>Public Property Let Payment(ByVal vPayment As
|
||
Double)<BR> gPayment = -(vPayment)<BR>End Property<BR><BR>Public
|
||
Property Get PresentSavings() As Variant<BR> PresentSavings =
|
||
gPresentSavings<BR>End Property<BR><BR>Public Property Let
|
||
PresentSavings(ByVal vPresentSavings As Variant)<BR> gPresentSavings =
|
||
-(vPresentSavings)<BR>End Property<BR><BR>Public Property Get WhenDue() As
|
||
Variant<BR> WhenDue = gWhenDue<BR>End Property<BR><BR>Public Property
|
||
Let WhenDue(ByVal vWhenDue As Variant)<BR> gWhenDue = vWhenDue<BR>End
|
||
Property<BR><BR></CODE>
|
||
<LI>All server components require an entry (starting) point. This is the code
|
||
that will be called when the object is first instantiated with
|
||
<b>Server.CreateObject</b> Your ASPTut component does
|
||
not have to do anything special when it is first called. For this reason, you
|
||
can provide an empty <b>Sub Main</b> procedure. From the <B>Project</B> menu,
|
||
select <B>Add Module</B>. In the <B>Add Module</B> window, under the
|
||
<B>New</B> tab, select the <B>Module</B> icon and click <B>Open</B>. In the
|
||
<B>Module 1</B> code window, type <B>Sub Main</B> and hit <B>Enter</B>. An
|
||
empty sub is created for you. </FONT>
|
||
<LI><FONT face=Verdana,Arial,Helvetica>Save your <b>Sub Main</b> module as
|
||
<B>Main.bas</B>. Save your class file as <B>Finance.cls</B>. Save your project
|
||
as <B>ASPTut.vbp</B>.
|
||
<LI>Click <B>File</B>, and click <B>Make ASPTut.dll</B>. This compiles and
|
||
registers the ASPTut.dll. After you have called ASPTut.dll from an ASP page,
|
||
you cannot make the DLL in Visual Basic until you unload the application in
|
||
which the ASP file is running. One way to do this is to use the <a href="iiabuti.htm">IIS snap-in</a>
|
||
to open the properties on your default Web site and click the
|
||
<B>Unload</B> button. If you want to register your DLL on another Web server,
|
||
copy ASPTut.dll to the server, click <B>Start</B>, click <B>Run</B>, and type
|
||
<B>cmd</B> into the <B>Open</B> text box. In the same directory as ASPTut.dll,
|
||
type <B>regsvr32 ASPTut.dll</B>.
|
||
<LI>Exit Visual Basic. </LI></OL>
|
||
<H3>Create an ASP Page to Use Your Visual Basic COM Object</H3>
|
||
<P>This example ASP page uses a form to read in user data, creates an instance
|
||
of your object, and calculates the future value of your savings plan. </P>
|
||
<P>Copy and paste the following code in your text editor, and save the file as
|
||
<B>CalculateFutureValue.asp</B> in the <I>x</I>:\Inetpub\Wwwroot\Tutorial
|
||
directory. View the example with your browser by typing
|
||
<B>http://localhost/Tutorial/CalculateFutureValue.asp </B>in the address bar.
|
||
</P><CODE>
|
||
<P> <%@ Language=VBScript %><BR><BR> <%<BR>
|
||
Response.Expires = 0<BR> Payment = Request.Form("Payment")<BR>
|
||
AnnualIntRate = Request.Form("AnnualIntRate")<BR> NumPayPeriods =
|
||
Request.Form("NumPayPeriods")<BR> WhenDue =
|
||
Request.Form("WhenDue")<BR> PresentSavings =
|
||
Request.Form("PresentSavings")<BR> %><BR><BR>
|
||
<HTML><BR> <HEAD><TITLE>Future Value
|
||
Calculation</TITLE></HEAD><BR> <BODY><BR> <FONT
|
||
FACE="MS Gothic"><BR><BR> <H2 align=center>Calculate the Future
|
||
Value of a Savings Plan</H2> <BR><BR> <FORM METHOD=POST
|
||
ACTION="calculatefuturevalue.asp"> <BR> <TABLE cellpadding=4
|
||
align=center> <BR> <TR><BR> <TD>How much do you
|
||
plan to save each month?</TD><BR> <TD><INPUT TYPE=TEXT
|
||
NAME=Payment VALUE=<%=Payment%>> (Required)</TD><BR>
|
||
</TR><TR><BR> <TD>Enter the annual interest
|
||
rate.</TD><BR> <TD><INPUT TYPE=TEXT NAME=AnnualIntRate
|
||
VALUE=<%=AnnualIntRate%>> (Required)</TD><BR>
|
||
</TR><TR> <BR> <TD>For how many months will you
|
||
save?</TD><BR> <TD><INPUT TYPE=TEXT NAME=NumPayPeriods
|
||
VALUE=<%=NumPayPeriods%>> (Required)</TD><BR>
|
||
</TR><TR> <BR> <TD>When do you make payments in the
|
||
month? </TD><BR> <TD><INPUT TYPE=RADIO NAME=WhenDue VALUE=1
|
||
<%If 1=WhenDue Then Response.Write"CHECKED"%>>Beginning <BR>
|
||
<INPUT TYPE=RADIO NAME=WhenDue VALUE=0 <%If 0=WhenDue Then
|
||
Response.Write"CHECKED"%>>End </TD><BR>
|
||
</TR><TR> <BR> <TD>How much is in this savings
|
||
account now?</TD><BR> <TD><INPUT TYPE=TEXT
|
||
NAME=PresentSavings VALUE=<%=PresentSavings%>> </TD><BR>
|
||
</TR><BR> </TABLE><BR> <P align=center><INPUT
|
||
TYPE=SUBMIT VALUE=" Calculate Future Value "><BR>
|
||
</FORM> <BR><BR> <%<BR> If ("" = Payment) Or ("" =
|
||
AnnualIntRate) Or ("" = NumPayPeriods) Then<BR><BR>
|
||
Response.Write "<H3 align=center>No valid input entered
|
||
yet.</H3>"<BR><BR> ElseIf (IsNumeric(Payment)) And
|
||
(IsNumeric(AnnualIntRate)) And (IsNumeric(NumPayPeriods))
|
||
Then<BR><BR> Dim FutureValue<BR> Set oASPTut
|
||
= Server.CreateObject("ASPTut.Finance")<BR>
|
||
oASPTut.AnnualIntRate = CDbl(AnnualIntRate)<BR>
|
||
oASPTut.NumPayPeriods = CInt(NumPayPeriods)<BR>
|
||
oASPTut.Payment = CDbl(Payment)<BR> If Not "" = PresentSavings
|
||
Then oASPTut.PresentSavings = CDbl(PresentSavings)<BR>
|
||
oASPTut.WhenDue = WhenDue<BR> FutureValue =
|
||
oASPTut.CalcFutureValue<BR> Response.Write "<H3
|
||
align=center>Future value = $" & FutureValue &
|
||
"</H3>"<BR><BR> Else<BR><BR> Response.Write
|
||
"<H3 align=center>Some of your values are not
|
||
numbers.</H3>"<BR><BR> End If<BR><BR> %><BR><BR>
|
||
</FONT> <BR> </BODY> <BR> </HTML>
|
||
</P></CODE>
|
||
<P>In the browser, you should see the following: </P>
|
||
<P><FONT face="MS Gothic">
|
||
<H2 align=center>Calculate the Future Value of a Savings Plan</H2>
|
||
<FORM method=post>
|
||
<TABLE align=center cellPadding=4>
|
||
<TBODY>
|
||
<TR>
|
||
<TD>How much do you plan to save each month?</TD>
|
||
<TD><INPUT name=Payment> (Required)</TD></TR>
|
||
<TR>
|
||
<TD>Enter the annual interest rate.</TD>
|
||
<TD><INPUT name=AnnualIntRate> (Required)</TD></TR>
|
||
<TR>
|
||
<TD>For how many months will you save?</TD>
|
||
<TD><INPUT name=NumPayPeriods> (Required)</TD></TR>
|
||
<TR>
|
||
<TD>When do you make payments in the month? </TD>
|
||
<TD><INPUT name=WhenDue type=radio value=1>Beginning <INPUT CHECKED
|
||
name=WhenDue type=radio value=0>End </TD></TR>
|
||
<TR>
|
||
<TD>How much is in this savings account now?</TD>
|
||
<TD><INPUT name=PresentSavings> </TD></TR></TBODY></TABLE>
|
||
<P align=center><INPUT type=button value=" Calculate Future Value "> </FORM>
|
||
<H3 align=center>No valid input entered yet.</H3></FONT>
|
||
<H2><A name=creatingj>Lesson 4: Creating a Java COM Object</A></H2>
|
||
<P>In this lesson, you use Microsoft<66> Visual J++<2B> to create a COM object which
|
||
does the same thing as the Visual Basic component in Lesson 3. This step requires Visual J++ 6.0
|
||
or later. </P>
|
||
<H3>Create the Java COM Object</H3>
|
||
<OL>
|
||
<LI>Open Visual J++. If you don't see a window titled <B>New Project</B>,
|
||
click the <B>File</B> menu and click <B>New Project</B>.
|
||
<LI>Select <B>Visual J++ Projects</B>, and click the <B>Empty Project</B>
|
||
icon. In the <B>Name</B> text box, type <B>ASPTut</B>. Click <B>Open</B>.
|
||
<LI>In the <B>Project</B> menu, click <B>Add Class</B>. In the <B>Name</B>
|
||
text box, type <B>ASPTut.java</B>. The class name must be the same as the
|
||
project name for a Java server component. Click <B>Open</B>. The
|
||
following should appear in a text editing window:<BR><BR><CODE>public class
|
||
ASPTut<BR>{<BR>}<BR><BR></CODE>
|
||
<LI>Copy the following code, and paste it between the brackets {}. Watch
|
||
capitalization because Java is case-sensitive. The following is a method in
|
||
your component:<BR><BR><CODE>public double CalcFutureValue(<BR> double
|
||
dblAnnualIntRate,<BR> double dblNumPayPeriods,<BR> double
|
||
dblPayment,<BR> double dblPresentSavings,<BR> boolean
|
||
bWhenDue)<BR>{<BR> double dblRet, dblTemp, dblTemp2, dblTemp3,
|
||
dblIntRate;<BR><BR> if (dblAnnualIntRate == 0.0)<BR>
|
||
{<BR> dblRet = -dblPresentSavings - dblPayment *
|
||
dblNumPayPeriods;<BR> }<BR> else<BR> {<BR>
|
||
dblIntRate = dblAnnualIntRate / 100 / 12;<BR> dblPayment =
|
||
-dblPayment;<BR> dblPresentSavings =
|
||
-dblPresentSavings;<BR><BR> dblTemp = (bWhenDue ? 1.0 +
|
||
dblIntRate : 1.0);<BR> dblTemp3 = 1.0 +
|
||
dblIntRate;<BR> dblTemp2 = Math.pow(dblTemp3,
|
||
dblNumPayPeriods); <BR> dblRet = -dblPresentSavings *
|
||
dblTemp2 - dblPayment * dblTemp * (dblTemp2 - 1.0) / dblIntRate;<BR>
|
||
}<BR><BR> return dblRet;<BR>}</CODE><BR><BR>
|
||
<LI>From the <B>Build</B> menu, click <B>Build</B>. Look in the <B>Task
|
||
List</B> window below the text editing window to see whether any errors are
|
||
generated.
|
||
<LI>The Java class file must be registered on the same machine as the Web
|
||
server. In a command window, find the ASPTut.class file that was built. It is
|
||
most likely either in %USERPROFILE%\My Documents\Visual Studio Projects\ASPTut or in
|
||
<I>x</I>:\Documents and Settings\<I>user name</I>\My Documents\Visual Studio
|
||
Projects\ASPTut, where x: is the drive on which you installed Windows. Copy
|
||
ASPTut.class to <I>x</I>:\Winnt\Java\Trustlib. Type <B>javareg /register
|
||
/class:ASPTut /progid:MS.ASPTut.Java</B>, and press <b>ENTER</b> to register the Java
|
||
class.
|
||
<LI>Close Visual J++. </LI></OL>
|
||
<H3>Create an ASP Page to Use Your Java COM Object</H3>
|
||
<P>This example ASP page uses a form to read in user data, creates an instance
|
||
of your object, and calculates the future value of your savings plan. This
|
||
example uses JScript, but you can call a Java component from VBScript as well.
|
||
</P>
|
||
<P>Copy and paste the following code in your text editor, and save the file as
|
||
<B>CalculateFutureValueJava.asp</B> in the <I>x</I>:\Inetpub\wwwroot\Tutorial
|
||
directory. View the example with your browser by typing
|
||
<B>http://localhost/Tutorial/CalculateFutureValueJava.asp</B> in the address
|
||
bar. </P><CODE> </CODE><CODE><%@ Language=JScript
|
||
%><BR><BR></CODE><CODE> </CODE><CODE><%<BR></CODE><CODE>
|
||
</CODE><CODE>Response.Expires = 0;<BR></CODE><CODE> </CODE><CODE>Payment =
|
||
Request.Form("Payment");<BR></CODE><CODE> </CODE><CODE>AnnualIntRate =
|
||
Request.Form("AnnualIntRate");<BR></CODE><CODE> </CODE><CODE>NumPayPeriods
|
||
= Request.Form("NumPayPeriods");<BR></CODE><CODE> </CODE><CODE>WhenDue =
|
||
Request.Form("WhenDue");<BR></CODE><CODE> </CODE><CODE>PresentSavings =
|
||
Request.Form("PresentSavings");<BR></CODE><CODE>
|
||
</CODE><CODE>%><BR><BR></CODE><CODE>
|
||
</CODE><CODE><HTML><BR></CODE><CODE>
|
||
</CODE><CODE><HEAD><TITLE>Future Value Calculation -
|
||
Java</TITLE></HEAD><BR></CODE><CODE>
|
||
</CODE><CODE><BODY><BR></CODE><CODE> </CODE><CODE><FONT FACE="MS
|
||
Gothic"><BR><BR></CODE><CODE> </CODE><CODE><H2
|
||
align=center>Calculate the Future Value of a Savings
|
||
Plan</H2> <BR><BR></CODE><CODE> </CODE><CODE><FORM
|
||
METHOD=POST
|
||
ACTION="calculatefuturevaluejava.asp"> <BR></CODE><CODE>
|
||
</CODE><CODE><TABLE cellpadding=4
|
||
align=center> <BR></CODE><CODE>
|
||
</CODE><CODE><TR><BR></CODE><CODE> </CODE><CODE><TD>How much
|
||
do you plan to save each month?</TD><BR></CODE><CODE>
|
||
</CODE><CODE><TD><INPUT TYPE=TEXT NAME=Payment
|
||
VALUE=<%=Payment%>> (Required)</TD><BR></CODE><CODE>
|
||
</CODE><CODE></TR><TR><BR></CODE><CODE>
|
||
</CODE><CODE><TD>Enter the annual interest
|
||
rate.</TD><BR></CODE><CODE> </CODE><CODE><TD><INPUT
|
||
TYPE=TEXT NAME=AnnualIntRate VALUE=<%=AnnualIntRate%>>
|
||
(Required)</TD><BR></CODE><CODE>
|
||
</CODE><CODE></TR><TR> <BR></CODE><CODE>
|
||
</CODE><CODE><TD>For how many months will you
|
||
save?</TD><BR></CODE><CODE> </CODE><CODE><TD><INPUT
|
||
TYPE=TEXT NAME=NumPayPeriods VALUE=<%=NumPayPeriods%>>
|
||
(Required)</TD><BR></CODE><CODE>
|
||
</CODE><CODE></TR><TR> <BR></CODE><CODE>
|
||
</CODE><CODE><TD>When do you make payments in the month?
|
||
</TD><BR></CODE><CODE> </CODE><CODE><TD><INPUT TYPE=RADIO
|
||
NAME=WhenDue VALUE=1 <%if (1==WhenDue)
|
||
Response.Write("CHECKED")%>>Beginning <BR></CODE><CODE>
|
||
</CODE><CODE><INPUT TYPE=RADIO NAME=WhenDue VALUE=0 <%if (0==WhenDue)
|
||
Response.Write("CHECKED")%>>End </TD><BR></CODE><CODE>
|
||
</CODE><CODE></TR><TR> <BR></CODE><CODE>
|
||
</CODE><CODE><TD>How much is in this savings account
|
||
now?</TD><BR></CODE><CODE> </CODE><CODE><TD><INPUT
|
||
TYPE=TEXT NAME=PresentSavings VALUE=<%=PresentSavings%>>
|
||
</TD><BR></CODE><CODE>
|
||
</CODE><CODE></TR><BR></CODE><CODE>
|
||
</CODE><CODE></TABLE><BR></CODE><CODE> </CODE><CODE><P
|
||
align=center><INPUT TYPE=SUBMIT VALUE=" Calculate Future Value
|
||
"><BR></CODE><CODE>
|
||
</CODE><CODE></FORM> <BR><BR></CODE><CODE>
|
||
</CODE><CODE><%<BR><BR></CODE><CODE> </CODE><CODE>if (("" == Payment)
|
||
|| ("" == AnnualIntRate) || ("" == NumPayPeriods)) {<BR><BR></CODE><CODE>
|
||
</CODE><CODE>Response.Write("<H3 align=center>No valid input
|
||
entered yet.</H3>");<BR><BR></CODE><CODE> </CODE><CODE>} else
|
||
{<BR><BR></CODE><CODE> </CODE><CODE>AnnualIntRate =
|
||
parseFloat(AnnualIntRate)<BR></CODE><CODE>
|
||
</CODE><CODE>NumPayPeriods = parseFloat(NumPayPeriods)<BR></CODE><CODE>
|
||
</CODE><CODE>Payment = parseFloat(Payment)<BR></CODE><CODE>
|
||
</CODE><CODE>if ("" != PresentSavings) PresentSavings =
|
||
parseFloat(PresentSavings);<BR><BR></CODE><CODE> </CODE><CODE>if
|
||
((isNaN(Payment)) || (isNaN(AnnualIntRate)) || (isNaN(NumPayPeriods)))
|
||
{<BR><BR></CODE><CODE> </CODE><CODE>Response.Write("<H3
|
||
align=center>Some of your values are not
|
||
numbers.</H3>");<BR><BR></CODE><CODE> </CODE><CODE>} else
|
||
{<BR><BR></CODE><CODE> </CODE><CODE>var FutureValue,
|
||
Cents;<BR></CODE><CODE> </CODE><CODE>var oASPTut =
|
||
Server.CreateObject("MS.ASPTut.Java");<BR></CODE><CODE>
|
||
</CODE><CODE>FutureValue = oASPTut.CalcFutureValue(AnnualIntRate, NumPayPeriods,
|
||
Payment, PresentSavings, WhenDue);<BR><BR></CODE><CODE>
|
||
</CODE><CODE>Response.Write("<H3 align=center>Future value = $" +
|
||
parseInt(FutureValue) + "</H3>");<BR><BR></CODE><CODE>
|
||
</CODE><CODE>}<BR></CODE><CODE> </CODE><CODE>}<BR></CODE><CODE>
|
||
</CODE><CODE>%><BR><BR></CODE><CODE>
|
||
</CODE><CODE></FONT> <BR></CODE><CODE>
|
||
</CODE><CODE></BODY> <BR></CODE><CODE>
|
||
</CODE><CODE></HTML><BR></CODE>
|
||
<P></P>
|
||
<P>In the browser, you should see the following content, which should be identical to the display generated using the Visual Basic
|
||
component in Lesson 3 of this module: </P>
|
||
<P><FONT face="MS Gothic">
|
||
<H2 align=center>Calculate the Future Value of a Savings Plan</H2>
|
||
<FORM method=post>
|
||
<TABLE align=center cellPadding=4>
|
||
<TBODY>
|
||
<TR>
|
||
<TD>How much do you plan to save each month?</TD>
|
||
<TD><INPUT name=Payment> (Required)</TD></TR>
|
||
<TR>
|
||
<TD>Enter the annual interest rate.</TD>
|
||
<TD><INPUT name=AnnualIntRate> (Required)</TD></TR>
|
||
<TR>
|
||
<TD>For how many months will you save?</TD>
|
||
<TD><INPUT name=NumPayPeriods> (Required)</TD></TR>
|
||
<TR>
|
||
<TD>When do you make payments in the month? </TD>
|
||
<TD><INPUT name=WhenDue type=radio value=1>Beginning <INPUT CHECKED
|
||
name=WhenDue type=radio value=0>End </TD></TR>
|
||
<TR>
|
||
<TD>How much is in this savings account now?</TD>
|
||
<TD><INPUT name=PresentSavings> </TD></TR></TBODY></TABLE>
|
||
<P align=center><INPUT type=button value=" Calculate Future Value "> </FORM>
|
||
<H3 align=center>No valid input entered yet.</H3></FONT>
|
||
<br>
|
||
<H2>Up Next: Maintaining Session State in a Web Application</H2>
|
||
<HR class=iis SIZE=1>
|
||
|
||
<P align=center><a href="/iishelp/common/colegal.htm"><EM><EFBFBD> 1997-2001
|
||
Microsoft Corporation. All rights reserved.</EM></A></P></FONT></BODY></HTML>
|