<h1><a name="module3writingyourownactivexservercomponents">Module 3: Writing Your Own Components</a></h1>
<p><a href="iiselect.asp?LessonFile=<%= Server.URLEncode(LessonFile)%>"><strong>Choose a scripting language for this lesson.</strong></a></p>
<p>In <a href="iiatmd2.asp">Module 2</a> you familiarized yourself with the components Active Server Pages (ASP) provides, now it's time to think about creating your own components, components that meet your specific needs.</p>
<p>Suppose you want create an ASP application that provides users with access to specific financial functions through your Web site. ASP does not explicitly provide such functionality, but getting it is as easy as creating your own Java or ActiveX component, which you will do in this module.</p>
<p>To get started, use the following form to select a component that you would like to learn how to create and implement:</p>
<hr>
<h3><A NAME="H3_22047542">Select a component to create for this lesson:</A></h3>
<li>To save and view your work in this module, you require the following:
<ul>
<li>Write and Script Web server permissions for the <%= VirtFilePath %> virtual directory on the <%= Request.ServerVariables("SERVER_NAME")%> Web server (with Active Server Pages installed). For more information, see <a href="/iishelp/iis/htm/core/iiwspsc.htm">Setting Web Server Permissions</a>.</li>
<li>Your Windows NT account must have Windows NT File System (NTFS) permissions that enable you to read, write, and run programs in the
<%
If ComponentPref = "JavaComp" Then
%> <em>DriveLetter</em>:\Winnt\Java directory and its subdirectories, where <em>DriveLetter</em> is the letter mapped to the appropriate drive of the <%= Request.ServerVariables("SERVER_NAME")%> Web server.</li>
<%Else%><em>DriveLetter</em>:\Winnt\System32\Inetsrv\ where you replace <em>DriveLetter</em> with the letter mapped to the appropriate drive of the <%= Request.ServerVariables("SERVER_NAME")%> Web server. (If you did not accept the default installation directory, substitute the name of your installation directory for \Winnt\System32.)</li>
<% End If %>
</ul>
<li>To complete this module, on the <%= Request.ServerVariables("SERVER_NAME") %> Web server you must have installed: </li>
<ul>
<li><% If ComponentPref = "JavaComp" Then
%>Microsoft® Visual J++ 1.1.
<%Else%>Either the 32-bit version of Microsoft® Visual Basic 5.0 Professional Edition or Visual Basic 5.0 Enterprise Edition development system.
<% End If %></li>
</ul>
<li>If you have disabled your Web browser's ability to run Java programs, enable this feature before proceeding with the tutorial.
</ol>
<hr>
<h2><a name="creatingthefinancecomponent">Lesson 1: Creating the <%
If ComponentPref = "JavaComp" Then
%>Java
<%Else%>ActiveX
<% End If %> Finance Component</a></h2>
<% If ComponentPref = "JavaComp" Then%>
<p>A component should contain a set of related methods (functions) that provide added value beyond what is in the scripting language that will be calling it. Because <%= ScriptLanguagePreference %> does not provide financial functions, you must explicitly create a finance function. Specifically, for this tutorial you will learn how to create a function that computes the future value of an investment, which returns an annuity based on a fixed interest rate and periodic, fixed payments.</p>
<p>With this declaration, you define the a finance function, belonging to the Finance class, that computes the future value of an investment. <!--</p>--><br>
<li>Click <strong>OK</strong>. The following appears in the Visual J++ text editor:</li>
<p>Don't be intimidated by this code, it simply defines the mathematical equation (in Java's syntax) necessary for computing the future value of an investment. If you are interested, you can find information about this commonly used equation in almost any financial mathematics or accounting text book.<!--</p>--><br>
<p><strong>Note</strong> Java is case sensitive, so if you decide to type the previous lines of code into the text editor, be sure to type Java syntax and variables exactly as shown.<!--</p>--><br>
</ol>
<h3><A NAME="H3_22053702">Build the Finance Component</A></h3>
<ol>
<li>In the <strong>Build</strong> menu, Select <strong>Build Finance</strong>. </li>
<li>In the <strong>Build</strong> window (located below the <strong>ClassView</strong> and text editing windows) make sure the build process has not generated any errors or warnings.</li>
</ol>
<h3><A NAME="H3_22054061">Copy the Finance Component to the Trustlib directory</A></h3>
<ol>
<li>Click the Windows <strong>Start</strong> button, point to <strong>Programs</strong>, then click <strong>Windows Explorer</strong>.</li>
<li>Copy the Finance.class component from the Program Files\DevStudio\MyProjects\Finance\ directory to the <%= Request.ServerVariables("SERVER_NAME") %> Web server's Winnt\Java\Trustlib directory.</li>
</ol>
<h3><a name="registerthefinanceservercomponent">Register the Finance Java Class</a></h3>
<p>You must register the Finance Java class to make it callable by JScript and all of the other OLE-compatible languages on your computer.<!--</p>--><br>
<ol>
<li>Open a command-prompt window.</li>
<li>Type <strong>cd <em>Drive Letter</em>:\<%= Request.ServerVariables("SERVER_NAME")%>\Program Files\DevStudio\VJ\Bin\</strong> at the command prompt, where <em>DriveLetter</em> is the letter mapped to the appropriate drive on your computer. (If you did not accept the default installation directory, substitute the name of your installation directory for \Program Files\DevStudio\VJ\Bin\.)</li>
<li>Click the <strong>OK</strong> button when a dialog box appears that says <strong>Successfully Registered Java class "Finance" as CLSID</strong>. </li>
<li>Close the command-prompt window.</li>
</ol>
<%Else%>
<p>A component should contain a set of related methods (functions) that provide added value beyond what is in the scripting language that will be calling it. Because <%= ScriptLanguagePreference %> does not provide financial functions, you must give access to the Visual Basic finance functions through your Finance server component. This server component could expose all of the Visual Basic finance functions including the <strong>DDB</strong> function (double-declining balance), <strong>FV</strong> function (future value), <strong>IPmt</strong> function (interest payment), <strong>IRR</strong> function (internal rate of return), and others. However, for this tutorial you will only the implement the <strong>FV</strong> function, which returns an annuity based on a fixed interest rate and periodic, fixed payments. <!--</p>--><br>
<p><strong>Note</strong> Selecting this option indicates that the project is intended to run without user interaction and should have no user interface elements.<!--</p>--><br>
<li>Click <strong>OK</strong>.</li>
</ol>
<p>The project is now named MS. Later, you will reference the Finance server component as <font face="courier" size="3"><code>MS.Finance</font></code> from an ASP script. <!--</p>--><br>
<h3><a name="addthefinanceclasstotheproject">Add the Finance Class to the Project</a></h3>
<p>In Visual Basic, to create a component with a set of functionality you can call, you define a <em>class</em>. A class groups methods and properties. In your project, it will be the place within which you specify your finance methods.<!--</p>--><br>
<ol>
<li>Press F4 to open the <strong>Properties</strong> window for the class module, then double-click the <strong>Name</strong> property and type <strong>Function</strong>, then press ENTER.</li>
<li>Click the arrow, then select <strong>5 - MultiUse</strong>. </li>
</ol>
<h3><a name="learnmoreaboutvisualbasicfinancefunctions">Learn More About Visual Basic Finance Functions</a></h3>
<p>The Visual Basic Help system describes available financial functions.<!--</p>--><br>
<ol>
<li>Click <strong>Help</strong>. </li>
<li>Select <strong>Microsoft Visual Basic Help Topics</strong>.</li>
<li>Select the <strong>Index</strong> tab, then type <strong>financial functions</strong> as the word to look for.</li>
<li>Double-click the <strong>financial functions</strong> index entry. </li>
<li>Click <strong>FV Function</strong> to learn more about it.</li>
<li>Close the <strong>Visual Basic Help</strong> dialog box when you have finished reviewing the finance functions.</li>
</ol>
<h3><a name="addthecalcfvfunctiontothefinanceclass">Add the CalcFV Function to the Finance Class</a></h3>
<p>The Finance server component does require some programming code. This code will make the Visual Basic built-in future value function available to languages making use of your component. <!--</p>--><br>
<p>Copy and paste the following lines into the Finance class code window:<!--</p>--><br>
<h3><a name="addthecomponentsentrypoint">Add the Components Entry Point</a></h3>
<p>All server components require an entry (starting) point. This is the code that will be called when the object is first made available to a language. In VBScript, when you use <strong>Server.CreateObject</strong>, an instance is created of an object. When the <strong>Server.CreateObject </strong>statement is executed, the <strong>Sub Main</strong> procedure in a server component (that is, one created with Visual Basic) is called.</p>
<p>Your finance component does not have to do anything special to initialize itself when it is called. For that reason, you can provide an empty (no Visual Basic statements) <strong>Sub</strong> <strong>Main</strong> procedure. <!--</p>--><br>
<ol>
<li>In the <strong>Project </strong>menu, select <strong>Add Module</strong>.</li>
<li>In the <strong>Module 1</strong>dialog box, double-click the module icon.</li>
<li>In the Module 1 code window, type <strong>Sub Main</strong>.</li>
<li>Press Enter.</li>
</ol>
<p>This automatically enters the following code:<!--</p>--><br>
<pre><font face="courier" size="3">Sub Main()
End Sub</font></pre>
<h3><a name="savethefinanceproject">Save the Finance Project</a></h3>
<p>When you save your work, you will be asked to save all three parts of the Visual Basic project. These include the project file, the class
module, and the code module.<!--</p>--><br>
<ol>
<li>Open the <strong>File </strong>menu.</li>
<li>Select <strong>Save Project</strong>.</li>
<li>In the <strong>File name</strong> text box, type Finance. Select the following path on the <%= Request.ServerVariables("SERVER_NAME")%> Web server: <strong><em>DriveLetter</em>:\Winnt\System32\Inetsrv\</strong> where <em>DriveLetter</em> is the letter mapped to the appropriate drive. (If you did not accept the default installation directory, substitute the name of your installation directory for \Winnt\System32.)</li>
<li>Click the <strong>Save</strong> button.</li>
<p>If a previous user has completed this portion of the tutorial, a message will appear stating that the file already exists. Save your version of the file in place of the older version.<!--</p>--><br>
<li>Double-click the value <strong>Project1</strong> in the <strong>File name</strong> text box to select it.</li>
<li>Type the name <strong>Finance</strong> for the Project file (.vbp).</li>
<li>Click the <strong>Save</strong> button to save the project.</li>
<p>If a previous user has completed this portion of the tutorial, a message will appear stating that the file already exists. Save your version of the file in place of the older version.<!--</p>--><br>
</ol>
<h3><a name="makethecomponentaninprocesscomponent">Make the Component an In-Process Component</a></h3>
<p>Visual Basic allows you to create in-process ActiveX components (formerly called OLE Automation Servers) and out-of-process ActiveX components. An <em>in-process</em> ActiveX component is a dynamic-link library (file name extension .dll) that is loaded by the calling process. An <em>out-of-process</em> ActiveX component is an executable (file name extension .exe) that runs as a separate process from the calling application. Because in-process components are in the same process space as the calling program, they provide better performance than out-of-process components.</p>
<p>To make the Finance server component an in-process ActiveX component</p>
<li>Click the <strong>Options</strong> button. </li>
<li>Select the <strong>Auto Increment</strong> check box. </li>
<li>Click <strong>OK</strong>. </li>
<li>Type <strong><em>DriveLetter</em>:\<%= Request.ServerVariables("SERVER_NAME")%>\Winnt\System32\Inetsrv\Finance</strong> where <em>DriveLetter</em> is the letter mapped to the appropriate drive on your computer. (If you did not accept the default installation directory, substitute the name of your installation directory for \Winnt\System32.)<br>
If a previous user has completed this portion of the tutorial, a message will appear stating that the file already exists. Save your version of the file in place of the older version.</li>
<li>Exit Visual Basic.</li>
</ol>
<h3><a name="registerthefinanceservercomponent">Register the Finance Server Component</a></h3>
<p>All server components must be registered. Windows NT and Windows 95 make use of the system registry to keep track of what server
components are available for use. By registering the Finance server component, you make it callable by VBScript and all of the other
OLE-compatible languages on your computer.</p>
<ol>
<li>Open a command-prompt window.</li>
<li>Type <strong>cd <em>Drive Letter</em>:\<%= Request.ServerVariables("SERVER_NAME")%>\Winnt\System32\Inetsrv</strong> at the command prompt, where <em>DriveLetter</em> is the letter mapped to the appropriate drive on your computer. (If you did not accept the default installation directory, substitute the name of your installation directory for \Winnt\System32.)</li>
<li>Click the <strong>OK</strong> button when a dialog box appears that says <strong>DllRegisterServer in finance.dll succeeded</strong>. </li>
<li>Close the command-prompt window.</li>
</ol>
<hr>
<% End If %>
<h2> <a name="callingthefinancecomponentfromascript">Lesson 2: Calling the Finance Component from a Script</a></h2>
<p>To test the component, you can call the component from Active Server Pages (ASP), Visual Basic, Microsoft® Office products that use Visual Basic for Applications, or any other OLE Automation controller.</p>
<p>To call the Finance server component from Active Server Pages by using <%=ScriptLanguagePreference%>, you can use an HTML form as input to calculate the future
value of a persons savings plan.</p>
<h3><a name="thehtmlform">The HTML Form</a></h3>
<p>An HTML form will be used to gather values that describe a savings plan. These values are assigned variables that are made available to an
ASP script as part of the <strong>Request</strong> object. You can reference a value from an HTML form. For example, the annual percentage
rate entered on a form can be referenced by a script using <font face="courier" size="3"><strong><code>Request("APR")</code></strong></font>. The HTML tag <font face="courier" size="3"><code><INPUT TYPE=TEXT NAME=APR></font></code> provides
the input field necessary to enter a value.</p>
<p>To send the form to a Microsoft Web server running ASP, the user presses a Submit button. The Submit button calls the page indicated by the <font face="courier" size="3"><code>ACTION</font></code> property of the HTML form tag. The HTML tag for the Submit button (<font face="courier" size="3"><code><INPUT
TYPE=SUBMIT VALUE=" Calculate Future Value "></font></code>) uses the value for <font face="courier" size="3"><code>ACTION</font></code> from the HTML form tag (<font face="courier" size="3"><code><FORM METHOD=POST
ACTION="Finance<%If ScriptLanguagePreference = "JScript" Then
%>j<%Else%><% End If %>.asp"></font></code>) to call the ASP page Finance<%
If ScriptLanguagePreference = "JScript" Then
%>j<%Else%><% End If %>.asp.</p>
<p>We have created the form for you. Use your text editor to open the file FVform.asp in the <%= Request.ServerVariables("SERVER_NAME")%> Web server's Tutorial directory (<%= FilePath %>).</p>
<h3><a name="thescript">The Script</a></h3>
<p>You use <%=ScriptLanguagePreference%> to call your Finance server component. The script starts by validating the inputs from the HTML form and assigning default values for any values that were not entered on the form. <%
If ScriptLanguagePreference = "JScript" Then
%>A custom function called <strong>IsNumeric</strong> tests whether or not a numeric (valid)
value was entered for each of the boxes on the HTML form. <%Else%>The VBScript <strong>IsNumeric</strong> function tests whether or not a numeric (valid)
value was entered for each of the boxes on the HTML form.</p>
<% End If %>
<%
If ComponentPref = "JavaComp" Then
%><p><strong>Server.CreateObject</strong> creates an instance of (that is, makes usable) your Finance component named <font face="courier" size="3"><code>MS.Finance.Java</font></code>. After creating an instance
of the component, you can make use of its methods and properties. The script line immediately following
<strong>Server.CreateObject</strong><%Else%>
<p><strong>Server.CreateObject</strong> creates an instance of (that is, makes usable) your Finance component named <font face="courier" size="3"><code>MS.Finance</font></code>. After creating an instance
of the component, you can make use of its methods and properties. The script line immediately following
<strong>Server.CreateObject</strong><% End If %> uses the <strong>CalcFV</strong> method to calculate a savings plan's future value. The result of this calculation appears on the the browser of the user requesting the information.</p>
<p>To view the script, use a text editor to open the file <%
If ScriptLanguagePreference = "JScript" Then
%>Financej.asp
<%Else%>Finance.asp
<% End If %> in the Tutorial directory (<%= FilePath %>).</p>
<h3><a name="usingyourbrowsertorunthetest">Using Your Browser to Run the Test</a></h3>
<p>To run the <%
If ScriptLanguagePreference = "JScript" Then
%>Financej.asp
<%Else%>Finance.asp
<% End If %> ASP page, open the FVform.asp file, which renders a form and then calls the <%
If ScriptLanguagePreference = "JScript" Then
%>Financej.asp<%Else%>Finance.asp<% End If %> script to calculate the future value of the savings plan specified on the form.</p>
<ol>
<li>Open FVform.htm by pointing your browser to <a href="http://<%=Request.ServerVariables("SERVER_NAME")%>/iishelp/iis/htm/tutorial/FVform.asp?ScriptLanguagePreference=<%=ScriptLanguagePreference%>&ComponentPref=<%=ComponentPref%>">http://<%=Request.ServerVariables("SERVER_NAME")%>/iishelp/iis/htm/tutorial/FVform.asp</a>.</li>
<li>Enter values for the form inputs in the <strong>Savings Plan</strong> form. <br></li>
<li>Click the <strong>Calculate Future Value</strong> button. The value of your savings plan should appear. </li>
</ol>
<p>In a relatively short time you have created a useful <%
If ComponentPref = "JavaComp" Then
%>Java<%Else%>ActiveX server<% End If %> component. If you need access to other financial functions, you can implement other financial functions <%
If ComponentPref = "JavaComp" Then
%>with Visual J++<%Else%>built into Visual Basic<% End If %> as additional methods of your Finance server component. We encourage you to experiment and come up with creative ways to utilize your own <%If ComponentPref = "JavaComp" Then%>Java<%Else%>ActiveX<% End If %> components. Soon, you will find that there is virtually no limit to the Web applications you can develop using components.</p>