%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.math.*" %>
<%@ page import="java.io.*" %>
<%@ page session="false" %>
<%
// the default 'out' does not support printf()...
PrintWriter m_out = response.getWriter();
// ---- requests without parameters trigger a redirect to the input form
// (not functional here, we just implement the test)
if(request.getParameter("name") == null)
{
String redir="HTTP/1.1 302 Found\r\n"
+ "Content-type:text/html\r\n"
+ "Location: loan.html\r\n\r\n"
+ ""
+ "
Redirect"
+ "Click HERE for redirect."
+ "";
m_out.println(redir);
}
else
{
// ---- process a GET query with parameters
long start = System.currentTimeMillis();
double payment, interest, principal, cost, amount, rate, term;
int month=0, year=1, lastpayment=1;
String[] Months={"January","February","March","April","May","June",
"July", "August","September","October","November","December"};
// the form field "names" we want to find values for
String szName="-", Name="", Amount="0.0", Rate="0.0", Term="0.0";
// get the form field values (note the ending '=' name delimiter)
Name = (String)request.getParameter("name");
Amount = (String)request.getParameter("amount");
Rate = (String)request.getParameter("rate");
Term = (String)request.getParameter("term");
// set default values if a parameter is missing
if(Name == null) Name ="-";
if(Amount == null) Amount ="100000.0";
if(Rate == null) Rate ="3.5";
if(Term == null) Term ="10.0";
// all litteral strings provided by a client must be escaped this way
// if you inject them into an HTML page
//public static String escape_html(String Name)
{
int len = Name.length();
StringBuffer sb = new StringBuffer(len);
boolean lastWasBlankChar = false;
int c;
for(int i=0; i') sb.append(">"); else
if(c == '\n') sb.append("<br/>");
else
{
c = c&0xffff; // unicode
if(c < 32 || c > 127)
{
sb.append("");
sb.append(new Integer(c).toString());
sb.append(';');
}
else
sb.append(c);
}
}
//return sb.toString();
szName = sb.toString();
}
// filter input data to avoid all the useless or nasty cases
amount = Double.parseDouble(Amount);
if(amount < 1.0)
amount = 1.0;
rate = Double.parseDouble(Rate);
if(rate > 19.0)
rate = 20.0;
if(rate > 1.0)
rate = rate/100.0;
else
if(rate < 1.0)
rate = 1.0/100.0;
term = Double.parseDouble(Term);
if(term < 0.1)
term = 1.0/12.0;
// do not do this in production...
//else
//if(term > 90.0)
// term = 90.0;
// calculate the monthly payment amount
payment = amount*rate/12*Math.pow(1+rate/12, term*12)
/ (Math.pow(1+rate/12, term*12)-1);
cost = (term*12*payment)-amount;
// build the top of our HTML page
String top=""
+ "Loan Calculator"
+ ""
+ "
Dear %s, your loan goes as follows:
";
m_out.printf(top, (szName.length()<2 || szName.charAt(0)=='-')
?"client":szName);
// DecimalFormat is rounding, don't know how to prevent this...
DecimalFormat df;
df = new DecimalFormat("0.00");
df.setGroupingUsed(true);
df.setGroupingSize(3);
m_out.printf("