/** ***************************************************************** calculate.java SWE 637 servlet example. @author Jeff Offutt @version 1.0 (Nov 2006) ********************************************************************* */ // Has two faults: // 1. If a string is submitted in the Length field, an exception will be returned. // 2. If two negative values are entered, the result will erroneously be negative. // Both faults have low exposure, and the second is easy to miss. // Import Java Libraries import java.io.*; import java.util.*; //Import Servlet Libraries import javax.servlet.*; import javax.servlet.http.*; // calculate class // CONSTRUCTOR: no constructor specified (default) // // *************** PUBLIC OPERATIONS ********************************** // public void doPost () --> // public void doGet () --> //*********************************************************************** public class calculate extends HttpServlet { // Location of servlet. static String Domain = "cs.gmu.edu:8443"; static String Path = "/offutt/servlet/"; static String Servlet = "calculate"; // Button labels static String OperationAdd = "Add"; static String OperationSub = "Subtract"; static String OperationMul = "Multiply"; static String OperationDiv = "Divide"; static String OperationLen = "Compute Length"; // Other strings. static String Style ="https://www.cs.gmu.edu/~offutt/classes/637/637-style.css"; /** ***************************************************** * Overrides HttpServlet's doPost(). * Converts the values in the form, performs the operation * indicated by the submit button, and sends the results * back to the client. ********************************************************* */ public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Float rslt = new Float (0.0); Float lhsVal = new Float (0.0); Float rhsVal = new Float (0.0); Float rsltVal = new Float (0.0); int rsltLen = 0; String operation = request.getParameter ("Operation"); String lhsStr = request.getParameter ("LHS"); String rhsStr = request.getParameter ("RHS"); String rsltStr = request.getParameter ("RSLT"); String nameStr = request.getParameter ("NAMESTRING"); String rsltLenStr = request.getParameter ("RSLTLEN"); response.setContentType ("text/html"); PrintWriter out = response.getWriter(); PrintHead (out); // Small bug .. the rsltLenStr is not used, but if a character is submitted, // then the servlet will crash. if ((rsltLenStr != null) && (rsltLenStr.length() > 0)) rsltLen = Integer.parseInt (rsltLenStr); try { if ((lhsStr != null) && (lhsStr.length() > 0)) lhsVal = new Float (lhsStr); if ((rhsStr != null) && (rhsStr.length() > 0)) rhsVal = new Float (rhsStr); if ((rsltStr != null) && (rsltStr.length() > 0)) rsltVal = new Float (rsltStr); if (operation.equals (OperationAdd)) rsltVal = new Float (lhsVal.floatValue() + rhsVal.floatValue()); else if (operation.equals (OperationSub)) rsltVal = new Float (lhsVal.floatValue() - rhsVal.floatValue()); else if (operation.equals (OperationMul)) {// Introduce a dumb but subtle bug. If both values are negative, // the value will erroneously be negative. // Hard to find and easy to miss. if ((lhsVal.floatValue() < 0.0) && (rhsVal.floatValue() < 0.0)) rsltVal = 0 - (new Float (lhsVal.floatValue() * rhsVal.floatValue())); else rsltVal = new Float (lhsVal.floatValue() * rhsVal.floatValue()); } else if (operation.equals (OperationDiv)) rsltVal = new Float (lhsVal.floatValue() / rhsVal.floatValue()); } catch (NumberFormatException e) { // nonnumeric, ask the user to try again. out.println ("
"); out.println ("One or more of your entries was not numeric."); out.println ("Please use only integers, no characters or spaces."); out.println ("
"); } if (operation.equals (OperationLen)) rsltLen = nameStr.length(); PrintBody (out, lhsStr, rhsStr, rsltVal.toString(), nameStr, rsltLen); PrintTail (out); } // End doPost /** ***************************************************** * Overrides HttpServlet's doGet(). * Prints an HTML page with a blank form. ********************************************************* */ public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType ("text/html"); PrintWriter out = response.getWriter(); PrintHead (out); PrintBody (out); PrintTail (out); } // End doGet /** ***************************************************** * Prints the of the HTML page, no . ********************************************************* */ private void PrintHead (PrintWriter out) { out.println (""); out.println (""); out.println (""); out.println ("637 Servlet Homework"); out.println (" "); out.println (""); out.println (""); } // End PrintHead /** ***************************************************** * Prints the of the HTML page with the form data * values from the parameters. ********************************************************* */ private void PrintBody (PrintWriter out, String lhs, String rhs, String rslt, String namestring, int rsltlen) { out.println (""); out.println ("

"); out.println ("A servlet to compute arithmetic operations on numbers and strings."); 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 ("
First val:"); out.println (" "); out.println ("
Second val:"); out.println (" "); out.println ("
Result:"); out.println (" "); out.println ("
"); out.println ("
"); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println (" "); out.println ("
Enter a name:"); out.println (" "); out.println ("
Length:"); if (rsltlen==0) out.println (" "); else 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 (""); } // End PrintBody /** ***************************************************** * Overloads PrintBody(out,lhs,rhs,rslt,namestring) to print a page * with blanks in the form fields. ********************************************************* */ private void PrintBody (PrintWriter out) { PrintBody (out, "", "", "", "", 0); } /** ***************************************************** * Prints the bottom of the HTML page. ********************************************************* */ private void PrintTail (PrintWriter out) { out.println (""); out.println ("

"); out.println ("Jeff Offutt, November 2006"); out.println ("

"); out.println (""); } // End PrintTail } // End calculate