Chili!ASP Loan Calculator Sample Application
If you haven't gone through the application itself yet, click on the link above to do so. Follow the links below for an explanation of the building of this application, and the Active Server fuctionality that went into it.
While not particularly robust, this simple application demonstrates a small part of the funtionality that Chili!ASP and Active Server Pages make possible. By simply adding database connectivity, it is easy to see how Active Server Pages can be used to build extremely powerful Web-based applications.
Basic things to know:
This documentation assumes at least a passing familiarity with both HTML and VBScript, which is, let's face it, all that the author has.
<% and %> are the
delimiters between which resides Active Server scripting.
A single quote ' within the ASP delimiters denotes a
comment/remark.
< and > are HTML tag delimiters.
<!-- and --> are HTML comment delimiters (see Server-side Include).
Simple HTML form front-end (login.htm)
First Look at ASP (setinfo.asp)
Server-side Include (common functions in one place)
Putting Common functions in one place (sitefunc.asp)
SubRoutines encapsulating ASP function
Using Variables Part I (loancalc.asp)
Using
Variables Part II (calculate.asp)
(or In Which We Call an External Object)
This is a simple application that demonstrates some of the powerful functionality that can be built into a web-based or intranet application using Active Server Pages functionality and Chili!ASP.
The application first logs you in (login.htm), taking some basic information (name, address, etc.) This information is maintained throughout your trip through the application by placing it into a Session Variable. The first page is all HTML, but it sets up the Active Server Pages that follow.
The ensuing page (setinfo.asp) is strictly ASP. It contains several different aspects of ASP, including Response.Buffer, Server-side include (actually a feature of HTML, but used here to encapsulate some SubRoutines that can be then called from more than one page without extra coding), Session Variables, the Request object and Response.Redirect.
Sitefunc.asp is the page that is included in the Server-side include above. It is made up of VBScript that is executed server-side, and includes SubRoutines that display error messages and check for blank fields in the entry form. It also features Response.Clear, Response.Write, Response.End and Request.ServerVariables.
The next page, (loancalc.asp) is mostly HTML, but it shows how Session Variables, stored from the first page, are still available, and can be displayed in the middle of standard HTML output. This page gathers the parameters for the Loan calculation that is the heart of this application.
The final page, (calculate.asp) contains a number of the ASP features we have already reviewed, plus Server.CreateObject, a call to an external object. In this case, the object is a simple ActiveX .dll that performs the calculations necessary to generate the amortization schedule that is ultimately output at the bottom of this page. This page shows how HTML and (Chili!)ASP can work together to create powerful applications on your web server.
This page, like all of the pages in this application, was originally coded by hand, then spruced up (as necessary) with Microsoft® FrontPage 97. The only things of note on this page are that the <form action> is set to point to Setinfo.asp, and that the method used is "Post" (see Post vs Get, below).
<!DOCTYPE HTML
PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Microsoft FrontPage 2.0"> <title>Chili!Soft Loan Calculator Login</title> </head> <body bgcolor="#FFFFFF" link="#FF0000"> <table border="0" width="100%"> <tr> <td><img src="chiliasp72.gif" width="216" height="54"></td> <td align="right"><img src="../aspbut.gif" width="100" height="30"></td> </tr> </table> <p> </p> <p><strong>Welcome</strong> to Chili!Soft's Loan Calculator Sample Application.<br> <br> This simple application will demonstrate some of the functionality that Chili!ASP makes possible through Active Server Pages. We will use, and later explain the use of, a few of the intrinsic objects of Chili!ASP, as well as demonstrating a call to an external object.<br> <br> For starters, please provide the following information about yourself. This will help demonstrate Chili!ASP's ability to store data throughout a session.<br> </p> <form action="setinfo.asp" method="post"> <table border="2" bordercolor="#000000" bordercolordark="#000000"> <tr> <td><table border="0"> <tr> <td>First Name:</td> <td><input type="text" size="30" name="fname"></td> </tr> <tr> <td>Last Name:</td> <td><input type="text" size="30" name="lname"></td> </tr> <tr> <td>Address 1:</td> <td><input type="text" size="30" name="address1"></td> </tr> <tr> <td>Address 2:</td> <td><input type="text" size="30" name="address2"></td> </tr> <tr> <td>City:</td> <td><input type="text" size="30" name="city"></td> </tr> <tr> <td>State:</td> <td><input type="text" size="30" name="state"></td> </tr> <tr> <td>Zip Code:</td> <td><input type="text" size="30" name="zip"></td> </tr> </table> </td> </tr> </table> <p><br> <input type="submit" value="Continue..."> </p> </form> </body> </html> |
When a form is submitted, and variables are being passed to the target page, you can use either the Get method, or the Post method. When using "Get", the variables and there values are appended to the end of the target URL, so that you get something like "http://smedley.com/setinfo.asp?fname=Larry&lname=Smith&..." in the address box of your browser. With "Post", the variables are passed behind the scenes. At the recieving end (in this case setinfo.asp) if the variables are sent with "Get" they are stored in the QueryString collection of the Request object. If "Post" is used, they are stored in the Form colletion. In either case, the values must be extracted by using the Request object.
In the cell below is the code from this page. I have tried to document the code within each page to explain what is happening where. Below the cell is a more in-depth explanation of some key points.
<% 'Buffer page
so nothing is displayed until all processing is complete Response.Buffer="TRUE" 'Server-side include--see sitefunc.asp %> <!--#include file="sitefunc.asp"--> <% 'Calls to SubRoutine CheckInput CheckInput "First Name", "fname" CheckInput "Last Name", "lname" CheckInput "Address 1", "address1" CheckInput "City", "city" CheckInput "State", "state" CheckInput "Zip", "zip" 'Set Session Variable Name = first and last names from previous form Session("Name")= Request("fname") & " " & Request("lname") 'Check to see if address2 has any value if( IsNull(Request("address2")) or Request("address2") = "" ) then 'If not, don't include it in Session Variable Address Session("Address")= Request("address1") & "<br>" & Request("city") & ", " & Request("state") & " " & Request("zip") ' If so, include it Else Session("Address")= Request("address1") & "<br>" & Request("address2") & "<br>" & Request("city") & ", " & Request("state") & " " & Request("zip") End If 'Redirect browser to loancalc.asp Response.Redirect "loancalc.asp" %> |
The Buffer property of the Response object can be set to either TRUE or FALSE (default). If is is FALSE, the output of the script is sent to the browser as it is processed. If, as in the case above, it is set to TRUE, the output is held until all processing is complete. In this case it is turned on to buffer output until script processing is complete, particularly script that is included in sitefunc.asp (see Response.Clear, Response.Write, Response.End)
Server-side
includes (SSI) are actually a part of HTML (note that the call
comes inside of an HTML comment, not as part of ASP scripting).
The file called in an SSI can be a place to store some cumbersome
HTML that you need to include on several pages, for example.
Instead of retyping or cutting and pasting the code each time,
you simply place it in its own file, and then include it wherever
necessary with an SSI call.
In ASP, as in this application, SSI works wonders as a single
lication in which to put SubRoutines that are called multiple
times or from different pages. The page that is the target of
this SSI (sitefunc.asp) contains two such
SubRoutines.
Values can be stored in the Session object. These values are then available throughout an entire session (or until a Session.Abandon is issued): across many different pages. Essentially, once a Session Variable is declared, it follows you from link to link until you leave the ASP application. In this application, we use Session("Name") and Session("Address") to store those values so that we can call them on different pages throughout the application.
The Request Object is used to retrieve values that the user input (in a form, for example). On this page (setinfo.asp) Request is used to retrieve the values that were entered in the form on login.htm. These values are stored in the Form collection (because the Post method was used--see Post vs. Get). If no collection is specified, as is the case in setinfo.asp) Request will search through all of the collections. Although the Request object can be used to retrieve values from other collections (e.g. Request.ServerVariables) retrieving from forms is probably the most common usage.
The lines of
code:
<%
Session("Name")= Request("fname") &
" " & Request("lname") %>
and
<%
Session("Address")= Request("address1") &
"<br>" & Request("city") &
", " & Request("state") & "
" & Request("zip") %>
show how variables and HTML
can be combined together into another variable. By setting the Session Variable "Name" equal to the First Name
from the form, plus a blank space (" "), plus the Last
Name from the form, we get a single variable whose value is
"fname lname", e.g. "Larry Smith". This makes
it easier to display the full name in the future.
The Redirect method of the Response object is one that you will use often in working with ASP. This method redirects the user's browser to another URL.
This is the page that is included as a result of the Server-side Include on some other pages. It includes the ShowError and CheckInput SubRoutines.
<script
language=vbscript runat=server> ' SubRoutine to display error messages-has 2 parameters Sub ShowError( Title, Message ) Response.Clear Response.Write "<html><head><title>Error!</title></head><body bgcolor=white>" ' Write out the Title value Response.Write "<h2>" & Title & "</h2>" ' Write out the Message value Response.Write Message & "<br><br>" ' Get the Server Variable HTTP_REFERER and set it as the target of the hyperlink Response.Write "<a href=" & Request.ServerVariables("HTTP_REFERER") & ">Go Back</a>" Response.End End Sub ' Subroutine to check if form fields were left blank Sub CheckInput( RealName, Name ) ' If they are blank if( IsNull(Request(Name)) or Request(Name) = "" ) then ' Call ShowError SubRoutine and display this error ShowError "The form was not completed","Please enter a value in the '" & realName & "' field." End If End Sub </script> |
Using SubRoutines, in a file that is part of a server-side include, allows you to code often-used functionality once and then call it from any page that "includes" the SubRoutine-containing page. Sitefunc.asp contains two such SubRoutines: ShowError and CheckInput. Each of these encapsulates several lines of (VBScript) ASP code that would have otherwise had to be re-entered several times to achieve the same functionality.
The ShowError SubRoutine accepts two parameters (Title and Message). Using the Reponse object's Clear, Write and End methods, and the ServerVariables property of the Request object, combined with some text and HTML tags, it takes the passed parameters and produces a simple HTML page that displays an error and a link back to the referring page. This SubRoutine can be called from any page that uses Sitefunc.asp as part of a Server-side include.
The CheckInput SubRoutine accepts two parameters (RealName and Name). Using a simple If statement, it checks to see if the form variable designated by Name is null or was left blank. If so, it calls the ShowError SubRoutine, and passes it the values necessary to produce an error message that explains to the user how to fix the problem, and gives them a link back to the page with the error.
The Clear method of the Response object will erase any buffered HTML. It causes an error if Response.Buffer is not set to TRUE.
The Write method of the Response object writes the specified information to the current HTML output.
The End method of the Response object stops processing of the script and sends the current output. It flushes the information buffered by Response.Buffer (when it is set to TRUE).
ServerVariables refers to a collection of environmental variables that are stored intrinsically by Chili!ASP. In this case, HTTP_REFERER returns the value in the Header "REFERER", which is the URL of the page the user just came from.
Using Variables Part I (Loancalc.asp)
This page contains mostly HTML, but it gives us our first opportunity to recall and display the values of some of the variables we have stored.
<!DOCTYPE HTML
PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Microsoft FrontPage 2.0"> <title>Chili!Soft Loan Calculator</title> </head> <body bgcolor="#FFFFFF" link="#FF0000"> <table border="0" width="100%"> <tr> <td><img src="chiliasp72.gif" width="216" height="54"></td> <td align="right"><img src="../aspbut.gif" width="100" height="30"></td> </tr> </table> <p> </p> <table border="0"> <tr> <td valign="top"><strong>Loan Calculation for:</strong></td> <% 'displays Name and Address--stored in Session Variables %> <td><strong><%= Session("Name")%><br> <%= Session("Address")%> </strong></td> </tr> </table> <p><br> Okay, we're ready to calculate monthly principal and interest payments and the amortization schedule for a loan. </p> <table border="2" bordercolor="#000000" bordercolordark="#000000"> <tr> <td><form action="calculate.asp" method="post"> <table border="0"> <tr> <td>Loan Amount/Principal (in dollars): $</td> <td><input type="text" size="20" name="amount"></td> </tr> <tr> <td>Loan Term (in years):</td> <td><input type="text" size="20" name="term">Years</td> </tr> <tr> <td>Interest Rate (in percent):</td> <td><input type="text" size="20" name="rate"></td> </tr> <tr> <td><input type="submit" value="Calculate"></td> </tr> </table> </form> </td> </tr> </table> <br><br> <!--#include file="..\srcform.inc"--> </body> </html> |
Including Variable values in HTML output
In order to
include a variable in the middle of some HTML, you simply delimit
the variable with <%= and %>, e.g.:
<font="courier">My
name is <%=Name%></font>
Calculate.asp is where we bring everything together and actually perform the proposed function of this application (producing an Loan Amortization schedule). In order to do so, we call an external object. In this case, it is a simple ActiveX .dll that takes a few parameters, allows you to set a few properties, and then executes the calculations and produces output, which is sent to the browser via a Response.Write call.
<% 'Buffer holds
all output until processing is complete RESPONSE.BUFFER="TRUE" 'Server-side include--see sitefunc.asp %> <!--#include file="sitefunc.asp"--> <% ' Set variables based on form fields and Session Variables P=Request("amount") I=Request("rate") L=Request("term") Name=Session("Name") Address=Session("Address") 'Calls to SubRoutine CheckInput CheckInput "Loan Amount", "amount" CheckInput "Interest Rate", "rate" CheckInput "Loan Term", "term" %> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Microsoft FrontPage 2.0"> <title>Payment Schedule for <%= Session("Name")%></title> </head> <body bgcolor="#FFFFFF" link="#FF0000"> <table border="0" width="100%"> <tr> <td><img src="chiliasp72.gif" width="216" height="54"></td> <td align="right"><img src="../aspbut.gif" width="100" height="30"></td> </tr> </table> <p> </p> <table border="0"> <tr> <td valign="top"><strong>Amortized Payment Schedule for:</strong></td> <% 'Include name and address again %> <td> <table cellpadding=5 cellspacing=0 border=1 width=325><tr> <td bgcolor=#dddddd><strong> <%= Name%><br> <%= Address%></strong> </td></tr> </table> </td></tr> </table> <p> </p> <table border="0"> <% ' Show values gathered from form %> <tr> <td width="200"><strong>Loan Amount: $<%= P%></strong></td> <td width="200"><strong>Interest Rate: <%= I%>%</strong></td> <td width="200"><strong>Term: <%= L%> Years</strong></td> </tr> </table> <% ' Call to create instance of external object set ob = Server.CreateObject( "CALC.AmSch" ) ' parameters passed to object ob.InitialAmount = P ob.InterestRate = I ob.LengthInYears = L ob.TableFormat = "border=1 width=85% cellspacing=0" ob.CellFormat = "bgcolor=#dddddd" ' call to execute object ob.Calculate %> <br><br> <!--#include file="..\srcform.inc"--> </body> </html> |
The Create.Object method is used to create an instance of an external (to Chili!ASP) object, or server component. In this case, the component is a simple ActiveX .dll, created in Visual Basic. Using it's properties, variables are passed and parameters are set, and then the Calculate method executes the object. The output (the HTML table that is the Amortization schedule) is stored in the ob.InterestPaid property, which is then written to the browser via Response.Write.