/** ***************************************************************** TwoButtons.java SWE 642 servlet example @author Jeff Offutt @version 1.0 (3/6/02) ********************************************************************* */ // Import Java Libraries import java.io.*; import java.util.*; //Import Servlet Libraries import javax.servlet.*; import javax.servlet.http.*; // TwoButtons class // CONSTRUCTOR: no constructor specified (default) // // *************** PUBLIC OPERATIONS ********************************** // public void doPost () --> prints a blank HTML page // public void doGet () --> prints a blank HTML page // private void PrintHead (PrintWriter out) --> Prints the HTML head section // private void PrintBody (PrintWriter out) --> Prints the HTML body with // the form. Fields are blank. // private void PrintBody (PrintWriter out, String lhs, String rhs, String rslt) // Prints the HTML body with the form. // Fields are filled from the parameters. // private void PrintTail (PrintWriter out) --> Prints the HTML bottom //*********************************************************************** public class TwoButtons extends HttpServlet { // Location of servlet. static String Domain = "ise.gmu.edu:8080"; static String Path = "/offutt/servlet/"; static String Servlet = "TwoButtons"; // Button labels static String OperationAdd = "Add"; static String OperationSub = "Subtract"; // Other strings. static String Style ="http://www.ise.gmu.edu/faculty/ofut/classes/642/642-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); String operation = request.getParameter ("Operation"); String lhsStr = request.getParameter ("LHS"); String rhsStr = request.getParameter ("RHS"); if ((lhsStr != null) && (lhsStr.length() > 0)) lhsVal = new Float (lhsStr); if ((rhsStr != null) && (rhsStr.length() > 0)) rhsVal = new Float (rhsStr); if (operation.equals (OperationAdd)) { rslt = new Float (lhsVal.floatValue() + rhsVal.floatValue()); } else if (operation.equals (OperationSub)) { rslt = new Float (lhsVal.floatValue() - rhsVal.floatValue()); } response.setContentType ("text/html"); PrintWriter out = response.getWriter(); PrintHead (out); PrintBody (out, lhsStr, rhsStr, rslt.toString()); 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 ("Name"); 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) { out.println (""); out.println ("

"); out.println ("A simple example that demonstrates how to operate with"); out.println ("multiple submit buttons."); out.println ("

"); out.print ("
"); 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 (""); out.println (""); } // End PrintBody /** ***************************************************** * Overloads PrintBody(out,lhs,rhs,rslt) to print a page * with blanks in the form fields. ********************************************************* */ private void PrintBody (PrintWriter out) { PrintBody (out, "", "", ""); } /** ***************************************************** * Prints the bottom of the HTML page. ********************************************************* */ private void PrintTail (PrintWriter out) { out.println (""); out.println ("

"); out.println ("Jeff Offutt, March 2002"); out.println ("

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