FileDocCategorySizeDatePackage
CalculatorServlet.javaAPI DocExample1924Sat Mar 15 19:39:50 GMT 2003com.oreilly.javaxp.cactus.servlet

CalculatorServlet

public class CalculatorServlet extends HttpServlet
author
Brian M. Coyner $version $Id: CalculatorServlet.java,v 1.4 2003/02/27 00:24:43 jepc Exp $

Fields Summary
public static final String
OPERAND_ONE
public static final String
OPERATION
public static final String
OPERAND_TWO
Constructors Summary
Methods Summary
protected voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


         
               
        verifyRequestParameters(req);
        // ... remainder of servlet logic goes here.
    
protected voidverifyRequestParameters(javax.servlet.http.HttpServletRequest req)


        try {
            Integer.parseInt(req.getParameter(OPERAND_ONE));
        } catch (NumberFormatException nfe) {
            // in reality we would use a RequestDispatcher to forward
            // to an error page.
            throw new ServletException(nfe.getMessage());
        }

        try {
            Integer.parseInt(req.getParameter(OPERAND_TWO));
        } catch (NumberFormatException nfe) {
            // in reality we would use a RequestDispatcher to forward
            // to an error page.
            throw new ServletException(nfe.getMessage());
        }

        String operator = req.getParameter(OPERATION);
        if (!("+".equals(operator) || "-".equals(operator))) {
            // in reality we would use a RequestDispatcher to forward
            // to an error page.
            throw new ServletException("Invalid Operator. " +
                                       "[operator=" + operator + "]");
        }