Creating a Visual J++ COM Component and Calling it from ASP
In this lesson, you use Microsoft ? Visual J++ ? to create a COM object which does the same thing as the Visual Basic component in Creating a Visual Basic COM Component and Calling it from ASP. This step requires Visual J++ 6.0 or later.
Create the Java COM Object
Open Visual J++. If you don't see a window titled New Project, click the File menu and click New Project.
Select Visual J++ Projects, and click the Empty Project icon. In the Name text box, type ASPTut. Click Open.
In the Project menu, click Add Class. In the Name text box, type ASPTut.java. The class name must be the same as the project name for a Java server component. Click Open. The following should appear in a text editing window:
public class ASPTut { }
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:
public double CalcFutureValue( double dblAnnualIntRate, double dblNumPayPeriods, double dblPayment, double dblPresentSavings, boolean bWhenDue) { double dblRet, dblTemp, dblTemp2, dblTemp3, dblIntRate; if (dblAnnualIntRate == 0.0) { dblRet = -dblPresentSavings - dblPayment * dblNumPayPeriods; } else { dblIntRate = dblAnnualIntRate / 100 / 12; dblPayment = -dblPayment; dblPresentSavings = -dblPresentSavings; dblTemp = (bWhenDue ? 1.0 + dblIntRate : 1.0); dblTemp3 = 1.0 + dblIntRate; dblTemp2 = Math.pow(dblTemp3, dblNumPayPeriods); dblRet = -dblPresentSavings * dblTemp2 - dblPayment * dblTemp * (dblTemp2 - 1.0) / dblIntRate; } return dblRet; }
From the Build menu, click Build. Look in the Task List window below the text editing window to see whether any errors are generated.
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 x:\Documents and Settings\ user name\My Documents\Visual Studio Projects\ASPTut, where x: is the drive on which you installed Windows. Copy ASPTut.class to x:\Winnt\Java\Trustlib. Type javareg /register /class:ASPTut /progid:MS.ASPTut.Java, and press ENTER to register the Java class.
Close Visual J++.
Create an ASP Page to Use Your Java COM Object
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.
Copy and paste the following code in your text editor, and save the file as CalculateFutureValueJava.asp in the x:\Inetpub\wwwroot\Tutorial directory. View the example with your browser by typing https://localhost/Tutorial/CalculateFutureValueJava.asp in the address bar.
<%@ Language= "JScript" %>
<%
Response.Expires = 0;
Payment = Server.HTMLEncode(Request.Form("Payment"));
AnnualIntRate = Server.HTMLEncode(Request.Form("AnnualIntRate"));
NumPayPeriods = Server.HTMLEncode(Request.Form("NumPayPeriods"));
WhenDue = Server.HTMLEncode(Request.Form("WhenDue"));
PresentSavings = Server.HTMLEncode(Request.Form("PresentSavings"));
%>
<HTML>
<HEAD>
<TITLE>Future Value Calculation - Java</TITLE>
</HEAD>
<BODY>
<FONT FACE="MS Gothic">
<H2 align=center>Calculate the Future Value of a Savings Plan</H2>
<FORM METHOD=POST ACTION="calculatefuturevaluejava.asp">
<TABLE cellpadding=4 align=center>
<TR>
<TD>How much do you plan to save each month?</TD>
<TD><INPUT TYPE=TEXT NAME=Payment VALUE=<%=Payment%>> (Required)</TD>
</TR><TR>
<TD>Enter the annual interest rate.</TD>
<TD><INPUT TYPE=TEXT NAME=AnnualIntRate VALUE=<%=AnnualIntRate%>> (Required)</TD>
</TR><TR>
<TD>For how many months will you save?</TD>
<TD><INPUT TYPE=TEXT NAME=NumPayPeriods VALUE=<%=NumPayPeriods%>> (Required)</TD>
</TR><TR>
<TD>When do you make payments in the month? </TD>
<TD><INPUT TYPE=RADIO NAME=WhenDue VALUE=1 <%if (1==WhenDue) Response.Write("CHECKED")%>>Beginning
<INPUT TYPE=RADIO NAME=WhenDue VALUE=0 <%if (0==WhenDue) Response.Write("CHECKED")%>>End </TD>
</TR><TR>
<TD>How much is in this savings account now?</TD>
<TD><INPUT TYPE=TEXT NAME=PresentSavings VALUE=<%=PresentSavings%>> </TD>
</TR>
</TABLE>
<P align=center><INPUT TYPE=SUBMIT VALUE=" Calculate Future Value ">
</FORM>
<%
if (("" == Payment) || ("" == AnnualIntRate) || ("" == NumPayPeriods)) {
Response.Write("<H3 align=center>No valid input entered yet.</H3>");
} else {
AnnualIntRate = parseFloat(AnnualIntRate)
NumPayPeriods = parseFloat(NumPayPeriods)
Payment = parseFloat(Payment)
if ("" != PresentSavings) PresentSavings = parseFloat(PresentSavings);
if ((isNaN(Payment)) || (isNaN(AnnualIntRate)) || (isNaN(NumPayPeriods)))
{
Response.Write("<H3 align=center>Some of your values are not numbers.</H3>");
}
else
{
var FutureValue, Cents;
var oASPTut = Server.CreateObject("MS.ASPTut.Java");
FutureValue = oASPTut.CalcFutureValue(AnnualIntRate, NumPayPeriods, Payment, PresentSavings, WhenDue);
Response.Write("<H3 align=center>Future value = $" + parseInt(FutureValue) + "</H3>");
}
}
%>
</FONT>
</BODY>
</HTML>
In the browser, you should see the following content:
Calculate the Future Value of a Savings Plan
How much do you plan to save each month? |
(Required) |
---|---|
Enter the annual interest rate. |
(Required) |
For how many months will you save? |
(Required) |
When do you make payments in the month? |
Beginning End |
How much is in this savings account now? |