- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
NOTE For information on creating, testing, and running servlets, see 31 in Java
NOTE For information on creating, testing, and running servlets, see 31 Creating QR Code 2d Barcode In Java Using Barcode generation for Java Control to generate, create QR Code JIS X 0510 image in Java applications. Scanning QR-Code In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. Part IV: Bar Code Generator In Java Using Barcode maker for Java Control to generate, create bar code image in Java applications. Barcode Reader In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. Applying Java
Creating QR Code 2d Barcode In C# Using Barcode drawer for Visual Studio .NET Control to generate, create Denso QR Bar Code image in .NET applications. Denso QR Bar Code Generation In VS .NET Using Barcode creator for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications. Converting the RegPay Applet into a Servlet
QR Code ISO/IEC18004 Generator In VS .NET Using Barcode maker for VS .NET Control to generate, create QR Code ISO/IEC18004 image in VS .NET applications. Print Quick Response Code In VB.NET Using Barcode encoder for .NET framework Control to generate, create Quick Response Code image in VS .NET applications. It is fairly easy to convert the RegPay loan calculating applet into a servlet First, the servlet must import the javaxservlet and javaxservlethttp packages It must also extend HttpServlet, not JApplet Next, you must remove all the GUI code Then, you must add the code that obtains the parameters passed to the servlet by the HTML that calls the servlet Finally, the servlet must send the HTML that displays the results The basic financial calculations remain the same It is only the way data is obtained and displayed that changes Drawing EAN 13 In Java Using Barcode generation for Java Control to generate, create EAN13 image in Java applications. UCC.EAN - 128 Creation In Java Using Barcode creator for Java Control to generate, create EAN128 image in Java applications. The RegPayS Servlet
EAN-13 Supplement 5 Creation In Java Using Barcode generator for Java Control to generate, create UPC - 13 image in Java applications. Barcode Generation In Java Using Barcode printer for Java Control to generate, create barcode image in Java applications. The following RegPayS class is the servlet version of the RegPay applet As the code is written, it assumes that RegPaySclass will be stored in Tomcat s example servlets directory, as described in 31 Remember to enter its name into the webxml file, also as described in 31 Making USPS Confirm Service Barcode In Java Using Barcode printer for Java Control to generate, create USPS Confirm Service Barcode image in Java applications. Reading Data Matrix In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. // A simple loan calculator servlet import javaxservlet*; import javaxservlethttp*; import javaio*; import javatext*; public class RegPayS extends HttpServlet { double principal; // original principal double intRate; // interest rate double numYears; // length of loan in years /* Number of payments per year You could allow this value to be set by the user */ final int payPerYear = 12; NumberFormat nf; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String payStr = ""; // Create a number format nf = NumberFormatgetInstance(); nfsetMinimumFractionDigits(2); nfsetMaximumFractionDigits(2); // Get String String String the parameters amountStr = requestgetParameter("amount"); periodStr = requestgetParameter("period"); rateStr = requestgetParameter("rate"); Bar Code Creation In Java Using Barcode generation for Eclipse BIRT Control to generate, create bar code image in BIRT applications. EAN 128 Maker In None Using Barcode creator for Microsoft Word Control to generate, create GS1-128 image in Word applications. try { if(amountStr != null && periodStr != null && rateStr != null) { principal = DoubleparseDouble(amountStr); UPC-A Supplement 2 Creator In VB.NET Using Barcode printer for .NET Control to generate, create Universal Product Code version A image in .NET framework applications. Draw EAN-13 Supplement 5 In Objective-C Using Barcode maker for iPhone Control to generate, create EAN-13 Supplement 5 image in iPhone applications. 32: EAN 13 Encoder In None Using Barcode printer for Software Control to generate, create EAN-13 image in Software applications. UCC-128 Generator In Java Using Barcode encoder for BIRT Control to generate, create USS-128 image in Eclipse BIRT applications. Financial Applets and Servlets
numYears = DoubleparseDouble(periodStr); intRate = DoubleparseDouble(rateStr) / 100; payStr = nfformat(compute()); } else { // one or more parameters missing amountStr = ""; periodStr = ""; rateStr = ""; } } catch (NumberFormatException exc) { // Take appropriate action here } // Set the content type responsesetContentType("text/html"); // Get the output stream PrintWriter pw = responsegetWriter(); // Display the necessary HTML pwprint("<html><body> <left>" + "<form name=\"Form1\"" + " action=\"http://localhost:8080/" + "servlets-examples/servlet/RegPayS\">" + "<B>Enter amount to finance:</B>" + " <input type=textbox name=\"amount\"" + " size=12 value=\""); pwprint(amountStr + "\">"); pwprint("<BR><B>Enter term in years:</B>" + " <input type=textbox name=\"period\""+ " size=12 value=\""); pwprintln(periodStr + "\">"); pwprint("<BR><B>Enter interest rate:</B>" + " <input type=textbox name=\"rate\"" + " size=12 value=\""); pwprint(rateStr + "\">"); pwprint("<BR><B>Monthly Payment:</B>" + " <input READONLY type=textbox" + " name=\"payment\" size=12 value=\""); pwprint(payStr + "\">"); pwprint("<BR><P><input type=submit value=\"Submit\">"); pwprintln("</form> </body> </html>"); } // Compute the loan payment double compute() { double numer; double denom; double b, e; numer = intRate * principal / payPerYear; Part IV: Applying Java
e = -(payPerYear * numYears); b = (intRate / payPerYear) + 10; denom = 10 - Mathpow(b, e); return numer / denom; } } The first thing to notice about RegPayS is that it has only two methods: doGet( ) and compute( ) The compute( ) method is the same as that used by the applet The doGet( ) method is defined by the HttpServlet class, which RegPayS extends This method is called by the server when the servlet must respond to a GET request Notice that it is passed a reference to the HttpServletRequest and HttpServletResponse objects associated with the request From the request parameter, the servlet obtains the arguments associated with the request It does this by calling getParameter( ) 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, a null is returned From the response object, 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 a PrintWriter to the response stream, the output type should be set to text/html by calling setContentType( ) RegPayS can 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, RegPayS calculates the loan payment and redisplays the form, with the payment field filled in Figure 32-7 shows the RegPayS servlet in action FIGURE 32-7
|
|