/** ***************************************************************** JOConvert2.java SWE 432 servlet assignment 2, Hwk 7. Modification of JConvert by Offutt and Li. @author Jeff Offutt @version 1.0 (10/24/00) ********************************************************************* */ // Import Servlet Libraries import javax.servlet.*; import javax.servlet.http.*; // Import Java Libraries import java.io.*; import java.util.*; import java.lang.*; // JOConvert2 class // // CONSTRUCTOR: no constructor specified (default) // // **************** PUBLIC OPERATIONS ********************************** // void doPost () --> Main servlet method for handling form // void doGet () --> Calls PrintHead and PrintForm (if called from a link) // void PrintHead () --> Regenerates the head of the web page // void PrintForm () --> Regenerates the form of the web page //************************************************************************* // // The possible IOException on the PrintWriter is thrown up. public class JOConvert2 extends HttpServlet { /** ***************************************************** * Overrides HttpServlet's doPost(). * Converts each entry in the form and prints the results * at the top of an HTML page. * The new values are printed in red (#FF0000). ********************************************************* */ public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType ("TEXT/HTML"); PrintWriter out = res.getWriter (); PrintHead (res); String FAsStr = req.getParameter ("F"); String CAsStr = req.getParameter ("C"); String inAsStr = req.getParameter ("in"); String cmAsStr = req.getParameter ("cm"); String ftAsStr = req.getParameter ("ft"); String mAsStr = req.getParameter ("m"); String miAsStr = req.getParameter ("mi"); String kmAsStr = req.getParameter ("km"); String galAsStr = req.getParameter ("gal"); String LAsStr = req.getParameter ("L"); String ozAsStr = req.getParameter ("oz"); String gAsStr = req.getParameter ("g"); String lbAsStr = req.getParameter ("lb"); String kgAsStr = req.getParameter ("kg"); int n; float num1, num2; // In version 1, we printed out the converted values. // Now we're going to save them in a container, // and write them into the form. Properties newvals = new Properties (); // out.println ("

"); // out.print (""); if (FAsStr != null && FAsStr.length ()>0) { // Convert farenheit num1 = (Float.valueOf (FAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) ( ( (num1-32.0)*5.0)/9.0); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); // Old: print the new values. // New: save value in property table. // p = newvals.setProperty ("C", String.valueOf(num2)); // We must still be using Java 1.2, which doesn't have // setProperty(). Use the Hashtable's put() newvals.put ("C", String.valueOf(num2)); } if (CAsStr != null && CAsStr.length ()>0) { // Convert Celsius num1 = (Float.valueOf (CAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) ( (num1*9.0/5.0)+32.0); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("F", String.valueOf(num2)); } if (inAsStr != null && inAsStr.length ()>0) { // Convert inches num1 = (Float.valueOf (inAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*2.54); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("cm", String.valueOf(num2)); } if (cmAsStr != null && cmAsStr.length ()>0) { // Convert centimeters num1 = (Float.valueOf (cmAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*0.3937); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("in", String.valueOf(num2)); } if (ftAsStr != null && ftAsStr.length ()>0) { // Convert feet num1 = (Float.valueOf (ftAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*0.3048); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("m", String.valueOf(num2)); } if (mAsStr != null && mAsStr.length ()>0) { // Convert meters num1 = (Float.valueOf (mAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1/0.3048); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("ft", String.valueOf(num2)); } if (miAsStr != null && miAsStr.length ()>0) { // Convert Miles num1 = (Float.valueOf (miAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*1.609); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("km", String.valueOf(num2)); } if (kmAsStr != null && kmAsStr.length ()>0) { // Convert kilometers num1 = (Float.valueOf (kmAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*0.6214); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("mi", String.valueOf(num2)); } if (galAsStr != null && galAsStr.length ()>0) { // Convert gallons num1 = (Float.valueOf (galAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*3.785); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("L", String.valueOf(num2)); } if (LAsStr != null && LAsStr.length ()>0) { // Convert Liters num1 = (Float.valueOf (LAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1/3.785); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("gal", String.valueOf(num2)); } if (ozAsStr != null && ozAsStr.length ()>0) { // Convert ounces num1 = (Float.valueOf (ozAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*28.35); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("g", String.valueOf(num2)); } if (gAsStr != null && gAsStr.length ()>0) { // Convert grams num1 = (Float.valueOf (gAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1/28.35); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("oz", String.valueOf(num2)); } if (lbAsStr != null && lbAsStr.length ()>0) { // Convert pounds num1 = (Float.valueOf (lbAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*0.4536); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("kg", String.valueOf(num2)); } if (kgAsStr != null && kgAsStr.length ()>0) { // Convert Kilograms num1 = (Float.valueOf (kgAsStr).floatValue ()); n = Math.round (num1* (float)100.0); num1 = (float) (n/ (float)100.0); num2 = (float) (num1*2.205); n = Math.round (num2* (float)100.0); num2 = (float) (n/ (float)100.0); newvals.put ("lb", String.valueOf(num2)); } // out.println ("
"); // // out.println ("


"); PrintForm (res, newvals); out.close (); } /** ***************************************************** * Overrides HttpServlet's doGet(). * Reprints the HTML page, blank. ********************************************************* */ public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Prints the form, added for completeness. res.setContentType ("TEXT/HTML"); PrintWriter out = res.getWriter (); PrintHead (res); // Needed for PrintForm, but empty is okay. Properties newvals = new Properties (); PrintForm (res, newvals); } /** ***************************************************** * Prints the head of the HTML page, without the form. ********************************************************* */ private void PrintHead (HttpServletResponse res) throws ServletException, IOException { PrintWriter out=res.getWriter (); out.println (""); out.println (""); out.println ("SWE 432: Measurement Conversion"); out.println (" "); out.println (""); out.println (""); out.println ("

On-line Measurement Conversion

"); out.println ("Jeff Offutt and Ren Li"); out.println ("
"); } /** ***************************************************** * Prints the form of the HTML page. * Also prints out the bottom and closes the PrintWriter. ********************************************************* */ private void PrintForm (HttpServletResponse res, Properties newvals) throws ServletException, IOException { // Reprints the form, just like the original HTML res.setContentType ("text/html"); PrintWriter out=res.getWriter (); out.print ("
"); out.println ("

"); out.println (" "); out.println (" "); out.print (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println ("
Fahrenheit (Fº): "); out.println (" <----->Celsius (Cº): "); out.println ("
Inch (in): "); out.println (" <----->Centimeter (cm): "); out.println ("
Feet (ft): "); out.println (" <----->Meter (m): "); out.println ("
Mile (mi): "); out.println (" <----->Kilometer (km): "); out.println ("
Gallon (gal): "); out.println (" <----->Liter (L): "); out.println ("
Ounce (oz): "); out.println (" <----->Gram (g): "); out.println ("
Pound (lb): "); out.println (" <----->Kilogram (kg): "); out.println ("
"); out.print (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println ("
"); out.println ("

"); out.println ("

"); out.println (""); out.println (" Class homepage"); out.println ("


"); // I guess this doesn't work remotely. // out.println (""); out.println ("

"); out.println (""); out.println ("© Jeff Offutt, 2000, all rights reserved."); out.println (" This document is made available for use by GMU graduate students of SWE 432."); out.println (" Copying, distribution or other use of this document"); out.println (" without express permission of the author is forbidden."); out.println (" You may create links to pages in this web site,"); out.println (" but may not copy all or part of the text without permssion"); out.println (" of the author."); out.println (""); out.println (""); out.println (""); out.println (""); out.close (); } }