No Slide Title

Download Report

Transcript No Slide Title

Chapter 20
The Shopping Cart
application
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 1
A Product page
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 2
The Index page
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 3
The Cart page
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 4
The User page
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 5
The Invoice page
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 6
The Credit Card page
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 7
The structure of the Shopping Cart application
index.jsp
music.cart package
cart.jsp
CartServlet
user.jsp
UserServlet
invoice.jsp
InvoiceServlet
SSL
creditcard.jsp
CompleteServlet
complete.jsp
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 8
The links from a Product page that call the
CartServlet class
<a href=
"../../servlet/music.cart.CartServlet?productCode=8601">
<img src="../../images/addtocart.gif" width="113"
height="47" border="0">
</a><br><br>
<a href="../../servlet/music.cart.CartServlet">
<img src="../../images/showcart.gif" width="113"
height="47" border="0">
</a><br><br>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 9
The index.jsp file
<h1>Quick order an album</h1>
<table cellpadding="5" border="0">
<tr valign="bottom">
<th align="left"><p>Description</th>
<th align="left"><p>Price</th>
<th align="left"><p>&nbsp;</th>
</tr>
<tr valign="top">
<td><p><a href="../albums/8601">
86 (the band) - True Life Songs and Pictures
</a></td>
<td><p>$14.95</td>
<td><p><a href=
"../servlet/music.cart.CartServlet?productCode=8601">
Add To Cart</a></td>
</tr>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 10
The index.jsp file (continued)
<tr valign="top">
<td><p><a href="../albums/pf01">
Paddlefoot - The first CD
</a></td>
<td><p>$12.95</td>
<td><p><a href=
"../servlet/music.cart.CartServlet?productCode=pf01">
Add To Cart</a></td>
</tr>
.
.
.
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 11
The CartServlet class
package music.cart;
import
import
import
import
import
import
import
javax.servlet.*;
javax.servlet.http.*;
java.io.*;
java.sql.*;
music.business.*;
music.data.*;
music.util.*;
public class CartServlet extends HttpServlet{
private MurachPool connectionPool;
public void init() throws ServletException{
connectionPool = MurachPool.getInstance();
}
public void destroy() {
connectionPool.destroy();
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 12
The CartServlet class (continued)
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
String productCode =
request.getParameter("productCode");
String quantityAsString =
request.getParameter("quantity");
HttpSession session = request.getSession(true);
Cart cart = (Cart) session.getAttribute("cart");
if (cart == null){
cart = new Cart();
session.setAttribute("cart", cart);
}
int quantity = 1;
try{
quantity = Integer.parseInt(quantityAsString);
if (quantity < 0)
throw new NumberFormatException();
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 13
The CartServlet class (continued)
catch(NumberFormatException nfe){
quantity = 1;
}
Connection connection =
connectionPool.getConnection();
try{
Product product =
ProductDB.readRecord(connection, productCode);
if (product != null){
LineItem lineItem = new LineItem(product,
quantity);
if (quantity > 0)
cart.addItem(lineItem);
else
cart.removeItem(lineItem);
}
}
catch(SQLException e){
log("CartServlet SQLException: " + e);
throw new ServletException(e);
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 14
The CartServlet class (continued)
finally{
connectionPool.freeConnection(connection);
}
session.setAttribute("cart", cart);
String url = "";
if (cart.getItems().size() <= 0){
url = "/cart/index.jsp";
}
else{
url = "/cart/cart.jsp";
}
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
doPost(request, response); }
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 15
The cart.jsp file
<%@ taglib uri="../WEB-INF/tlds/music.tld" prefix="msc" %>
<h1>Your shopping cart</h1>
<table cellspacing="5" border="0">
<tr>
<th align="left"><p>Qty</th>
<th align="left"><p>Description</th>
<th align="left"><p>Price</td>
<th align="left"><p>Amount</td>
</tr>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 16
The cart.jsp file (continued)
<msc:cart>
<tr valign="top">
<td><p>
<form action="<%=response.encodeURL(
"../servlet/music.cart.CartServlet")%>"
method="post">
<input type="hidden" name="productCode"
value="<%=productCode%>">
<input type=text size=2 name="quantity"
value="<%=quantity%>">
<input type="submit" value="Update">
</form>
</td>
<td><p><%=productDescription%></td>
<td><p><%=productPrice%></td>
<td><p><%=total%></td>
</tr>
</msc:cart>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 17
The cart.jsp file (continued)
<tr>
<td colspan="3">
<p><b>To change the quantity for an item</b>,
enter the new quantity and click on the Update
button.</p>
<p><b>To remove an item</b>, set the quantity to zero
and click on the Update button.</p>
</td>
</tr>
</table>
<form action="<%=response.encodeURL("../cart/index.jsp")%>"
method="post">
<input type="submit" value="Continue Shopping">
</form>
<form action="<%=response.encodeURL(
"../servlet/music.cart.UserServlet")%>" method="post">
<input type="submit" value="Checkout">
</form>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 18
The UserServlet class
package music.cart;
import
import
import
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.sql.*;
music.business.*;
music.data.*;
music.util.*;
public class UserServlet extends HttpServlet{
private MurachPool connectionPool;
public void init() throws ServletException{
connectionPool = MurachPool.getInstance();
}
public void destroy() {
connectionPool.destroy();
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 19
The UserServlet class (continued)
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
HttpSession session = request.getSession(true);
Cookie[] cookies = request.getCookies();
String emailAddress =
CookieUtil.getCookieValue(cookies, "emailCookie");
User user = null;
if (!(emailAddress == "")){
Connection connection =
connectionPool.getConnection();
try{
user = UserDB.readRecord(connection,
emailAddress);
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 20
The UserServlet class (continued)
if ( (user != null) &&
(!(user.getAddress1().equals("")) ) ){
session.setAttribute("user", user);
session.setAttribute("updateUserFields",
"no");
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(
"/servlet/music.cart.InvoiceServlet");
dispatcher.forward(request, response);
}
else{
session.setAttribute("user", user);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(
"/cart/user.jsp");
dispatcher.forward(request, response);
}
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 21
The UserServlet class (continued)
catch(SQLException e){
log("UserServlet SQLException: " + e);
throw new ServletException();
}
finally{
connectionPool.freeConnection(connection);
}
}
else{
user = new User();
session.setAttribute("user", user);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(
"/cart/user.jsp");
dispatcher.forward(request, response);
}
}
//a doPost method that calls doGet
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 22
The user.jsp file
<jsp:useBean id="user" class="music.business.User"
scope="session" />
<%
session.setAttribute("updateUserFields", "yes");
%>
<h1>Enter your name and contact information</h1>
<form action="../servlet/music.cart.InvoiceServlet"
method=post>
<table border="0" cellpadding="5">
<tr>
<td></td>
<td align=left><p>Required <font color=red>*</font></td>
</tr>
<tr>
<td align=right><p>First Name</td>
<td><input type="text" name="firstName" size="20"
maxlength=20 value="<jsp:getProperty
name="user" property="firstName"/>">
<font color=red>*</font></td>
</tr>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 23
The user.jsp file (continued)
<tr>
<td align=right><p>Last Name</td>
<td><input type=text name="lastName" size=20
value="<jsp:getProperty name="user"
property="lastName"/>">
<font color=red>*</font></td>
</tr>
.
.
.
<tr>
<td align=right><p>Zip Code</td>
<td><input type=text name="zip" size=20
value="<jsp:getProperty name="user"
property="zip"/>">
<font color=red>*</font></td>
</tr>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 24
The user.jsp file (continued)
<tr>
<td align=right><p>Country</td>
<td><input type=text name="country" size=20
value="<jsp:getProperty name="user"
property="country"/>">
<font color=red>*</font></td>
</tr>
<tr>
<td align=right><p>&nbsp;</td>
<td><input type="button" value="Continue"
onClick="validate(this.form)"></td>
</tr>
</table>
</form>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 25
The InvoiceServlet class
package music.cart;
import
import
import
import
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.text.*;
java.util.*;
music.business.*;
music.data.*;
music.util.*;
public class InvoiceServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
HttpSession session = request.getSession(true);
Cart cart = (Cart)session.getAttribute("cart");
User user = (User)session.getAttribute("user");
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 26
The InvoiceServlet class (continued)
String updateUserFields = (String)
session.getAttribute("updateUserFields");
if ( (updateUserFields != null) &&
(updateUserFields.equals("yes")) ){
String firstName =
request.getParameter("firstName");
String lastName =
request.getParameter("lastName");
String companyName =
request.getParameter("companyName");
String emailAddress =
request.getParameter("emailAddress");
String address1 =
request.getParameter("address1");
String address2 =
request.getParameter("address2");
String city = request.getParameter("city");
String state = request.getParameter("state");
String zip = request.getParameter("zip");
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 27
The InvoiceServlet class (continued)
String country =
request.getParameter("country");
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmailAddress(emailAddress);
user.setCompanyName(companyName);
user.setAddress1(address1);
user.setAddress2(address2);
user.setCity(city);
user.setState(state);
user.setZip(zip);
user.setCountry(country);
session.setAttribute("user", user);
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 28
The InvoiceServlet class (continued)
java.util.Date today = new java.util.Date();
DateFormat dateFormat =
DateFormat.getDateInstance();
String invoiceDateAsString =
dateFormat.format(today);
session.setAttribute("invoiceDateAsString",
invoiceDateAsString);
Invoice invoice = new Invoice();
invoice.setUser(user);
invoice.setInvoiceDate(today);
Vector lineItems = cart.getItems();
invoice.setLineItems(lineItems);
double total = invoice.getInvoiceTotal();
NumberFormat currency =
NumberFormat.getCurrencyInstance();
session.setAttribute("invoiceTotalAsString",
currency.format(total));
session.setAttribute("invoice", invoice);
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 29
The InvoiceServlet class (continued)
String url =
response.encodeURL("/cart/invoice.jsp");
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
doGet(request, response);
}
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 30
The invoice.jsp file
<%@ taglib uri="../WEB-INF/tlds/music.tld" prefix="msc" %>
<jsp:useBean id="user" class="music.business.User"
scope="session" />
<h1>Your invoice</h1>
<table border="0" cellspacing="5">
<tr><td><p><b>Date:</td>
<td width="400"><p>
<%=session.getAttribute("invoiceDateAsString")%>
</td>
<td></td>
</tr>
<tr valign="top">
<td><p><b>Ship To:</td>
<td><p><jsp:getProperty name="user"
property="firstName"/>
<jsp:getProperty name="user"
property="lastName"/><br>
<jsp:getProperty name="user"
property="companyName"/><br>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 31
The invoice.jsp file (continued)
<jsp:getProperty name="user"
property="address1"/><br>
<jsp:getProperty name="user"
property="address2"/><br>
<jsp:getProperty name="user" property="city"/>,
<jsp:getProperty name="user" property="state"/>
<jsp:getProperty name="user" property="zip"/><br>
<jsp:getProperty name="user"
property="country"/></td>
<td></td>
</tr>
<tr><td colspan="3"><hr></td></tr>
<tr><td><p><b>Qty</td>
<td><p><b>Description</td>
<td><p><b>Price</b></p></td>
</tr>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 32
The invoice.jsp file (continued)
<msc:cart>
<tr>
<td><p><%=quantity%></td>
<td><p><%=productDescription%></td>
<td><p><%=total%></td>
</tr>
</msc:cart>
<tr>
<td><p><b>Total:</td>
<td></td>
<td><p>
<%=(String)session.getAttribute("invoiceTotalAsString")%>
</td>
</tr>
</table>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 33
The invoice.jsp file (continued)
<form
action="<%=response.encodeURL("../cart/user.jsp")%>"
method="post">
<input type="submit" value="Edit Address">
</form>
<form action=
"<%=response.encodeURL("../cart/creditcard.jsp")%>"
method="post">
<input type="submit" value="Continue">
</form>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 34
The creditcard.jsp file
<h1>Enter your credit card information</h1>
<form action="<%=response.encodeURL(
"../servlet/music.cart.CompleteServlet")%>"
method="post">
<table border="0" cellpadding="5">
<tr>
<td align="right"><p>Credit card type</td>
<td><select name="creditCardType" size="1">
<option value="Visa">Visa</option>
<option value="Mastercard">Mastercard</option>
<option value="AmEx">American Express</option>
</select>
</td>
</tr>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 35
The creditcard.jsp file (continued)
<tr>
<td align="right"><p>Card number</td>
<td><p><input type="text" size="20"
name="creditCardNumber" maxlength="25"></td>
</tr>
<tr>
<td align="right"><p>Expiration date (mm/yyyy)</td>
<td><p><select name="creditCardExpirationMonth">
<option value="01">01
<option value="02">02
<option value="03">03
<option value="04">04
<option value="05">05
<option value="06">06
<option value="07">07
<option value="08">08
<option value="09">09
<option value="10">10
<option value="11">11
<option value="12">12
</select>/
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 36
The creditcard.jsp file (continued)
<select name="creditCardExpirationYear">
<option value="2003">2003
<option value="2004">2004
<option value="2005">2005
<option value="2006">2006
<option value="2007">2007
<option value="2008">2008
<option value="2009">2009
<option value="2010">2010
</select>
</p>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="left"><input type="submit"
value="Submit Order"></td>
</tr>
</table>
</form>
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 37
The CompleteServlet class
package music.cart;
import
import
import
import
import
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.sql.*;
java.text.DateFormat;
javax.mail.*;
music.business.*;
music.data.*;
music.util.*;
public class CompleteServlet extends HttpServlet{
private MurachPool connectionPool;
public void init() throws ServletException{
connectionPool = MurachPool.getInstance();
}
public void destroy() {
connectionPool.destroy();
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 38
The CompleteServlet class (continued)
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
HttpSession session = request.getSession(true);
User user = (User)session.getAttribute("user");
Invoice invoice =
(Invoice)session.getAttribute("invoice");
String creditCardType =
request.getParameter("creditCardType");
String creditCardNumber =
request.getParameter("creditCardNumber");
String creditCardExpMonth =
request.getParameter("creditCardExpirationMonth");
String creditCardExpYear =
request.getParameter("creditCardExpirationYear");
user.setCreditCardType(creditCardType);
user.setCreditCardNumber(creditCardNumber);
user.setCreditCardExpirationDate(creditCardExpMonth
+ "/" + creditCardExpYear);
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 39
The CompleteServlet class (continued)
Connection connection =
connectionPool.getConnection();
try{
boolean found =
UserDB.overwriteRecord(connection, user);
if (!found)
UserDB.writeRecord(connection, user);
InvoiceDB.writeRecord(connection, invoice);
}
catch(SQLException e){
log("CompleteServlet SQLException: " + e);
throw new ServletException();
}
finally{
connectionPool.freeConnection(connection);
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 40
The CompleteServlet class (continued)
Cookie emailCookie = new Cookie("emailCookie",
user.getEmailAddress());
emailCookie.setMaxAge(60*24*365*2*60);
emailCookie.setPath("/");
response.addCookie(emailCookie);
try{
String to = user.getEmailAddress();
String from =
"[email protected]";
String subject = "Order Confirmation";
String message = "Dear " + user.getFirstName()
+ ",\n\n"
+ "Thanks for ordering from us. You should "
+ "receive your order \n"
+ "in 3-5 business days. Please contact us "
+ "if you have any questions.";
MailUtil.sendMail(to, from, subject, message);
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 41
The CompleteServlet class (continued)
catch(MessagingException e){
System.out.println(
"CompleteServlet MessagingException: "
+ e.getMessage());
}
String url =
response.encodeURL("/cart/complete.jsp");
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
doGet(request, response);
}
}
Java Servlets and JSPCH20
© 2003, Mike Murach & Associates, Inc.
Slide 42