I am currently working my way through a Sam's Teach Yourself JSP. In one chapter, we wrote a crude shopping cart application which used Servlets for various functions. Come Chapter 14, we replace most of those servlets with JSP using beans.
For some reason that is eluding me, my JSP isn't working.
Here is the functional servlet:
/**
*
*/
package hu.flux.shoppingcart;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* @author Brian Kessler
*
*/
public class AddToShoppingCartServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@SuppressWarnings("deprecation")
public void service (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
// First get the item values from the request.
String productCode = request.getParameter("productCode");
String description = request.getParameter("description");
int quantity = Integer.parseInt(request.getParameter("quantity"));
double price = Double.parseDouble(request.getParameter("price"));
// Now create an item to add to the cart.
Item item = new Item(productCode, description, price, quantity);
HttpSession session = request.getSession();
ShoppingCart cart = ShoppingCart.getCart(session);
cart.addItem(item);
// Now display the cart and allow the user to check out or order more items.
response.sendRedirect(
response.encodeRedirectUrl(ShoppingCart.SHOPPING_CART_PATH + "/ShowShoppingCart.jsp"));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
and
here is the dysfunctional JSP:
<%-- Get a reference to the shopping cart --%>
<jsp:useBean id="cart" class="hu.flux.shoppingcart.ShoppingCart" scope="session" />
<%-- Create an item object --%>
<jsp:useBean id="item" class="hu.flux.shoppingcart.Item" scope="page" />
<%-- Copy the request parameters into the item --%>
<jsp:setProperty name="item" property="*"/>
<%-- Add the item to the shopping cart --%>
<% cart.addItem(item); %>
<%--Display the product catalog again --%>
<jsp:forward page="ShowShoppingCart.jsp" />
To help mystify things, no errors are displaying. The last line is definitely working as it successfully forwards to ShowShoppingCart.jsp, but then the cart is empty.
After experimenting, I have come to the conclusion that whatever is going wrong must happen when or after is called. I know this because I moved some code from a on ShowShoppingCart.jsp onto the AddToShoppingCart.jsp and (after some debugging), the shopping cart would display correctly on AddToShoppingCart.jsp
As it may help to understand what is going wrong, here are two more pieces of code:
*Here is the page called by jsp:forward page="ShowShoppingCart.jsp" *
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="hu.flux.shoppingcart.*"%>
<%
String shoppingCartSerialization = "";
ShoppingCart cart = ShoppingCart.getCart(session);
shoppingCartSerialization = cart.getSerialization();
Cookie cookie = new Cookie ("ShoppingCart", shoppingCartSerialization);
cookie.setMaxAge(999999999);
response.addCookie (cookie);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#ffffff">
<div><jsp:include page="DisplayShoppingCart.jsp" flush="true" /></div>
<form action="<%=ShoppingCart.SHOPPING_CART_PATH %>/Checkout.jsp"
method="post" >
You may <a
href="<%=ShoppingCart.SHOPPING_CART_PATH %>/ShowProductCatalog2.jsp"
>continue shopping</a> or
<input type="submit" value="Check Out">
</form>
<br/><br/><br/><hr/>
<form action="TerminateShoppingSessionServlet" method="post"><input
type="submit" value="Terminate Session"></form>
</body>
</html>
*Here is the page called by jsp:include page="DisplayShoppingCart.jsp" flush="true" *
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="hu.flux.shoppingcart.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%-- Show the header with the shopping cart item --%>
<img id="cart_image"
src="/SamsTeachYourselfJSP/ShoppingCart/images/cart4.png" />
<h1>Shopping Cart</h1>
<% // Get the current shopping cart from the user's session.
ShoppingCart cart = ShoppingCart.getCart(session);
// Get the items from the cart.
Vector<Item> items = cart.getItems();
// If there are no items, tell the user that the cart is empty.
if (items.size() == 0) {%><strong>Your shopping cart is empty.</strong>
<%}
else
{
%>
<%-- Display the header for the shopping cart table --%>
<br />
<table border="4">
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<%
int numItems = items.size();
// Get a formatter to write out currency values.
NumberFormat currency = NumberFormat.getCurrencyInstance();
for (int i=0; i<numItems; i++)
{
Item item = (Item)items.elementAt(i);
out.print (item.printShoppingCartTableRow(currency,
// "/SamsTeachYourselfJSP/ShoppingCart/RemoveItemServlet?item="+i)
"/SamsTeachYourselfJSP/ShoppingCart/RemoveItem.jsp?item="+i)
);
}
%>
</table>
<%
}
%>
Any ideas what I'm overlooking?