More on servlets
The case expression
The echo servlet will behave poorly however if you don't give it any parameter. It will usually just cause the browser to hang. To rectify this problem, we can use the following nicer echo servlet:
The case expression in general has the form
a with one of the values.
When a match is found, the corresponding result expression is
evaluated. If no match is found then the else clause applies and
the lastresult expression is evaluated. The niceEcho.servlet
shows how to use the case to support an interaction between the
user and the servlet.
Next we give an example of a servlet that handles many parameters:
Hi [name], so you are [age] years old!
} )Processing numbers using servlets
Let us now revisit and extend some of the first servlets we considered above. For example, lets generalize the hypotenuse servlet to handle any triangle provided we enter the lengths of the two other sides.
One way to write this servlet is the following:
(Double. a1) expression converts the input parameter a1 into a decimal number.
The let* syntax is a way of giving temporary names to calculated values. The names
are only valide "within" the let* expression.
The servlet parameters a1 and b1 must be strings that represent numbers
or else the servlet will generate an error! We will discuss
methods for dealing with user input errors in a later
section on "error checking". For now, we assume that the
user will follow instructions and enter numbers when they
are expected.
Another way is to use the "define" statement to give values to the variables "a" and "b" as follows:
Giving names to values using let*
When processing complex formulas it is often helpful to break the formula into pieces and to give names to each individual piece. For example, the body mass index provides a rough measure of whether your are overweight. The formula consists of dividing your weight in kilograms by the square of your height in meters. The following servlet computes your BMI by first converting your weight in pounds to your weight in kilograms, and your height in inches to your height in meters, then it applies the formula:
and your height is [h] inches,
Then your BMI is [bmi].
Note: a BMI over 30 is considered medically dangerous}))))
let* expression above provides temporary names
k, m, and bmi for the computed values.
The general form of a let* statement is
Exprs are evaluated and stored in the corresponding
variables var1,.... These variables are then used in the
final Expr to compute the value returned by the let*
expression. The key points to note here are that each variable can be
used to define the values of the following variables, but none of
the variables has a value outside of the let*. Note: there is
also a let expression which is almost exactly the same, except
that the defined variables cannot depend on each other.
You can also do this with define and begin:
and your height is [h] inches,
Then your BMI is [bmi].
Note: a BMI over 30 is considered medically dangerous}))))
let* expression to compute the area of
a triangle whose sides are a, b, and c using the following formula:
A = sqrt((s-a)(s-b)(s-c)s)where s=(a+b+c)/2 is the semiperimeter. You should try this with a=3, b=4, and c=5. These are the sides of a 3-4-5 right triangle and so the areas should be half of base times width, which is 0.5*3*4= 6.
The if form and conditional execution
As a final example of these simple servlets, we now consider a liquor test servlet which determines whether you can buy liquor in Massachusetts.
This servlet uses the (if TEST THEN ELSE form
to test whether the buyer's age is 21 or higher. This form
first evaluates the TEST. If the result is false, it
then evaluates the ELSE code, otherwise it evaluates
the THEN code. This example also introduces the
comparison operator <. Scheme has a rich set of
operators for creating tests. The numeric comparison
operators you can use are
a and b can be
arithmetic expressions (like (/ age 10)).
You can also combine tests using the and, or, and
not
operators, e.g. to test if someone is "college age" you
could use the following expression:
age.
The cond form and multiple tests
Sometimes one wants to combine several tests and do
something different for each of the possible test results.
For example, one might want to classify someone as
a child, teenager, adult, or senior citizen based on their
age. The simplest way to do this is using the cond
form, as in the following servlet
cond expression consists of several "clauses" each of which
begins with a test. If the test is true, then the rest of the clause is evaluated.
Otherwise, testing continues with the next clause. The very last clause should
always be an "else" clause which applies to all remaining cases. The tests can
be simple comparisons as shown above, or can be very complex expressions.
Summary of Scheme Syntax
The first figure below shows all of the Scheme expressions we have seen in this chapter and the next figure shows the Scheme procedures we have encountered thus far.where the
Ei are numbers, strings,
or S-expressions.
where
Ei are S-expressions
where
E is an S-expression
where the
Ci are constants and the Ei are expressions.
where
TEST, THEN, ELSE are expressions.
where
Ti are tests and the Ei are expressions.
where the
Vi are variables and the Ei are expressions.
