/** *****************************************************************
gcd.java servlet example for TJ Java class.
@author Joyce Su & Jeff Offutt
@version 1.0 (July 2009)
********************************************************************* */
// Import Java Libraries
import java.io.*;
import java.util.*;
//Import Servlet Libraries
import javax.servlet.*;
import javax.servlet.http.*;
// gcd class
// Based on the swing / awt version by Joyce Su
public class gcd extends HttpServlet
{
// Location of servlet.
static String Domain = "cs.gmu.edu:8443";
static String Path = "/offutt/servlet/";
static String Servlet = "gcd";
// Button labels
static String OperationLCM = "LCM";
static String OperationGCD = "GCD";
// Other strings.
/** *****************************************************
* Overrides HttpServlet's doPost().
* Converts the values in the form
* Performs the operation * indicated by the submit button
* Sends the results back to the client
********************************************************* */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
int rsltVal = 0;
int lhsVal = 0;
int rhsVal = 0;
String operation = request.getParameter("Operation");
String lhsStr = request.getParameter("LHS");
String rhsStr = request.getParameter("RHS");
String rsltStr = request.getParameter("RSLT");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
PrintHead(out);
try
{
if ((lhsStr != null) && (lhsStr.length() > 0))
lhsVal = Integer.parseInt (lhsStr);
if ((rhsStr != null) && (rhsStr.length() > 0))
rhsVal = Integer.parseInt (rhsStr);
if ((rsltStr != null) && (rsltStr.length() > 0))
rsltVal = Integer.parseInt (rsltStr);
if (operation.equals (OperationLCM))
rsltVal = lhsVal * rhsVal / gcd (lhsVal, rhsVal);
else if (operation.equals (OperationGCD))
rsltVal = gcd (lhsVal, rhsVal);
} 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("
");
}
PrintBody(out, lhsStr, rhsStr, (new Integer (rsltVal)).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, but not the .
********************************************************* */
private void PrintHead(PrintWriter out)
{
out.println("");
out.println("");
out.println("");
out.println("GCD example");
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 servlet to compute GCDs and LCMs");
out.println("");
out.print("");
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("Joyce Su & Jeff Offutt, July 2009");
out.println("
");
out.println("");
} // End PrintTail
/** *****************************************************
* Computes the greatest common divisor of two integers
* Written by Joyce Su
********************************************************* */
private int gcd(int a, int b)
{
int num=0;
if (a=1; k--)
{
if (a%k==0 && b%k==0)
{
num=k;
break;
}
}
}
if (b=1; p--)
{
if (b%p==0 && a%p==0)
{
num=p;
break;
}
}
}
return num;
}
} // End calculate