Java Reference
In-Depth Information
private void doRemove(HttpSession cart)
{
String currentProduct =
(String)cart.getAttribute("currentProd");
Object product =
cart.getAttribute(currentProduct);
//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.
if (product!=null)
//Product found in cart.
cart.removeAttribute(currentProduct);
}
}
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ā€¦
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import javax.servlet.annotation.WebServlet;
@WebServlet("/Checkout")
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,
HttpServletResponse response)
throws IOException,ServletException
{
HttpSession cart = request.getSession();
response.setContentType("text/HTML");
PrintWriter out = response.getWriter();
Search WWH ::




Custom Search