Java Reference
In-Depth Information
The only lines in the original
Weight
servlet requiring change are the class header
line and those lines specifying URLs. The changed lines (with changes indicated in
bold) are shown below.
public class Weight
X
extends HttpServlet
response.sendRedirect("ShoppingCart
X
.html");
(There are three occurrences of the above line.)
response.sendRedirect("
Checkout.jsp
");
File
WeightX.java
, which encapsulates this servlet, will need to be compiled
before running the application, of course.
Finally, we come to the code for the JSP corresponding to the
Checkout
servlet
(which, naturally enough, will be named
Checkout.jsp
). We can't use either
printf
(a method of the
PrintStream
class) or
format
(a method of the
PrintWriter
class) to
format our decimal output, since we have neither a
PrintStream
object nor a
PrintWriter
object that we can use. Consequently, we shall have to use the static
method
format
of the
String
class.
Since a reference to an object of class
Enumeration
(from package
java.util
) is
returned by the
Session
class's
getAttributeNames
method, we shall make use of the
import
attribute of the JSP directive
page
. As in
Selection.jsp
, use will be made of the
implicit JSP object
session
, rather than a
Session
object that has been created explicitly
by this application. Once again, the JSP-specifi c code is shown in bold type…
<!-- Checkout.jsp -->
<%@ page import="java.util.Enumeration" %>
<HTML>
<HEAD>
<TITLE>Checkout</TITLE>
<STYLE>
body{text-align=center; }
table,th,td{border:2px solid black;}
table{background-color:aqua;}
</STYLE>
</HEAD>
<BODY>
<%
fi nal fl oat APPLES_PRICE = 1.45F;
fi nal fl oat PEARS_PRICE = 1.75F;
//In a real application, the above prices would be
//retrieved from a database, of course.
%>
Search WWH ::
Custom Search