Java Reference
In-Depth Information
of a Web site provided entirely via servlets, the shopping cart example from the
previous chapter will be re-implemented.
Example
The initial page will be renamed ShoppingCart X .html . The only change required
for this page is the address for the form's ACTION attribute. Instead of specifying a
servlet called Selection , this will now specify a JSP called Selection.jsp :
<FORM METHOD=POST ACTION=" Selection.jsp ">
The code for Selection.jsp is shown below, with JSP-specifi c content shown in
bold. Note that, if the 'Checkout' option is selected by the user, control is now re-
directed to another JSP (viz., Checkout.jsp ), rather than to servlet Checkout . Note
also how use is made of the implicit object session to store the value of the current
product, without the necessity for creating a Session object explicitly (as was the
case in the selection servlet).
In the servlet-only version of this application, control is then passed to a Weight
servlet. Since this servlet's activities consist entirely of background processing and
re-direction to the next appropriate page, with no Web page output being generated,
this is an ideal opportunity for keeping the servlet. There are one or two minor
changes that need to be made to this servlet (as will be identifi ed shortly) and the
modifi ed servlet will be named WeightX . The reference to this servlet is also shown
in bold type below.
<!-- Selection.jsp -->
<HTML>
<HEAD>
<TITLE> <%= currentProduct %> </TITLE>
<STYLE>
body{text-align=center; color=blue;}
</STYLE>
</HEAD>
<BODY>
<%
String currentProduct;
currentProduct = request.getParameter("Product");
if (currentProduct.equals("Checkout"))
response.sendRedirect("Checkout.jsp");
else
session.setAttribute(
"currentProd",currentProduct);
%>
<H1><P STYLE=”color:red”> <%= currentProduct %>
</P></H1>
<BR><BR><BR>
Search WWH ::




Custom Search