private void doRemove(HttpSession cart)
String currentProduct =
Object product =
//Note that there is no need for a typecast into
//String, since we only need to know that there
//is an order for the current product in the cart.
//Product found in cart.
Once all product selections have been made and the 'Checkout' option has been
taken, the Checkout servlet will be executed. Before we look at the code for this
servlet, though, we need to consider the issue of formatting decimal output , since
the Checkout servlet needs to show costs to precisely two decimal places and to
allow a sensible maximum fi eld size. We can't use printf , since this is a member of
the PrintStream class, not of the PrintWriter class. However, the PrintWriter class
does have the equivalent method format and it is this method that we shall use.
Now for the Checkout servlet code…
public class Checkout extends HttpServlet
private fi nal fl oat APPLES_PRICE = 1.45F;
private fi nal fl oat PEARS_PRICE = 1.75F;
//In a real application, the above prices would be
//retrieved from a database, of course.
public void service(HttpServletRequest request,
HttpSession cart = request.getSession();
PrintWriter out = response.getWriter();