e = -(payPerYear * numYears);
b = (intRate / payPerYear) + 1.0;
denom = 1.0 - Math.pow(b, e);
return numer / denom;
}
}
The first thing to notice aboutRegPaySis that it has only two methods:doGet( )and
compute( ). Thecompute( )method is the same as that used by the applet. ThedoGet( )
method is defined by theHttpServletclass, whichRegPaySextends. This method is called by
the server when the servlet must respond to a GET request. Notice that it is passed a reference
to theHttpServletRequestandHttpServletResponseobjects associated with the request.
From therequestparameter, the servlet obtains the arguments associated with the
request. It does this by callinggetParameter( ). The parameter is returned in its string form.
Thus, a numeric value must be manually converted into its binary format. If no parameter
is available, anullis returned.
From theresponseobject, the servlet obtains a stream to which response information
can be written. The response is then returned to the browser by outputting to that stream.
Prior to obtaining aPrintWriterto the response stream, the output type should be set to
text/html by callingsetContentType( ).
RegPayScan be called with or without parameters. If called without parameters, the
servlet responds with the necessary HTML to display an empty loan calculator form.
Otherwise, if called with all needed parameters,RegPayScalculates the loan payment and
redisplays the form, with the payment field filled in. Figure 32-7 shows theRegPaySservlet
in action.
962 Part IV: Applying Java
FIGURE 32-7
TheRegPayS
ser vlet in action