A P P E N D I X  A

CDShopCart Source Files

This appendix displays the code for the following CDShopCart components:

This code is also available as source files within the CDShopCart application zip file, which you can download from the Forte for Java Developer Resources portal at:

http://forte.sun.com/ffj/documentation/tutorialsandexamples.html



Tip - If you use these files to cut and paste code into the Forte for Java 4 Source Editor, all formatting is lost. To automatically reformat the code in the Source Editor, select the code you want to reformat, then press Control-Shift F.




ProductList.jsp Source


<%@page contentType="text/html"%>
<html>
<head><title>CD Catalog List</title></head>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<body>
<h1> CD Catalog List </h1>
 
<sql:setDataSource var="productDS"
	url="jdbc:pointbase:server://localhost/cdshopcart"
	driver="com.pointbase.jdbc.jdbcUniversalDriver"
	user="PBPUBLIC" />
 
<%--<sql:setDataSource var="productDS"
	url="jdbc:oracle:thin:@<hostname>:<port#>:<SID>"
	driver="oracle.jdbc.driver.OracleDriver"
	user="userid" /> --%>
 
<%--<sql:setDataSource var="productDS"
	url="jdbc:weblogic:mssqlserver4:<database>@<hostname>:<port#>"
	driver="weblogic.jdbc.mssqlserver4.Driver"
	user="userid" /> --%>
 
<sql:query var="productQuery"  dataSource="${productDS}"  >
	SELECT * FROM CD 
</sql:query>
 
<TABLE border=1>
	<TR>
		<TH>ID</TH>
		<TH>CD Title</TH>
		<TH>Artist</TH>
		<TH>Country</TH>
		<TH>Price</TH>
	</TR>
 
<c:forEach var="row" items="${productQuery.rows}">
	<TR>
		<TD><c:out value="${row.ID}"/></TD>
		<TD><c:out value="${row.CDTITLE}"/></TD>
		<TD><c:out value="${row.ARTIST}"/></TD>
		<TD><c:out value="${row.COUNTRY}"/></TD>
		<TD><c:out value="${row.PRICE}"/></TD>
		<TD> 
		<form method=get action="ShopCart.jsp">
			<input type=hidden name=cdId value="<c:out value="${row.ID}"/>">
			<input type=hidden name=cdTitle value="<c:out value="${row.CDTITLE}"/>">
			<input type=hidden name=cdPrice value="<c:out value="${row.PRICE}"/>">
			<input type=submit name=operation value=Add>
		</form>
		</TD>
	</TR>
</c:forEach>
</TABLE>
</body>
</html>
 


CartLineItem Bean Source


package ShopCart;
 
import java.beans.*;
 
public class CartLineItem extends Object implements java.io.Serializable {
 
	private static final String PROP_SAMPLE_PROPERTY = "SampleProperty";
 
	private String sampleProperty;
 
	private PropertyChangeSupport propertySupport;
 
	/** Holds value of property cdtitle. */
	private String cdtitle;
 
	/** Holds value of property id. */
	private int id;
 
	/** Holds value of property price. */
	private double price;
 
	/** Creates new CartLineItem */
	public CartLineItem() {
		propertySupport = new PropertyChangeSupport ( this );
	}
 
	public String getSampleProperty() {
		return sampleProperty;
	}
 
	public void setSampleProperty(String value) {
		String oldValue = sampleProperty;
		sampleProperty = valie;
		propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
	}
 
	public void addPropertyChangeListener (PropertyChangeListener listener) {
		propertySupport.addPropertyChangeListener (listener);
	}
	public void removePropertyChangeListener (PropertyChangeListener listener){
		propertySupport.removePropertyChangeListener (listener);
	}
 
	/** Getter for property cdtitle.
	* @return Value of property cdtitle.
	*/
	public String getCdtitle() {
		return this.cdtitle;
	}
 
	/** Setter for property cdtitle.
	* @param cdtitle New value of property cdtitle.
	*/
	public void setCdtitle(String cdtitle) {
		this.cdtitle = cdtitle;
	}
 
	/** Getter for property id.
	* @return Value of property id.
	*/
	public int getId() {
		return this.id;
	}
 
	/** Setter for property id.
	* @param id New value of property id.
	*/
	public void setId(int id) {
		this.id = id;
	}
 
	/** Getter for property price.
	* @return Value of property price.
	*/ 
	public double getPrice() {
		return this.price;
	}
 
	/** Setter for property price.
	* @param price New value of property price.
	*/
	public void setPrice(double price) {
		this.price = price;
	}
 
	public void setId(java.lang.String pId) {
		int val = Integer.parseInt(pId);    // no error checking done here
		this.setId(val);
	}
 
	public void setPrice(java.lang.String pPrice) {
		double val = Double.parseDouble(pPrice);
		this.setPrice(val);
	}
}
 


CartLineItemBeanInfo Source


package ShopCart;
 
import java.beans.*;
 
public class CartLineItemBeanInfo extends SimpleBeanInfo {
 
	// Bean descriptor information will be obtained from introspection.
	private static BeanDescriptor beanDescriptor = null;
	private static BeanDescriptor getBdescriptor(){
 
		// Here you can add code for customizing the BeanDescriptor.
 
		return beanDescriptor;     } 
 
	// Properties information will be obtained from introspection.
	private static PropertyDescriptor[] properties = null;
	private static PropertyDescriptor[] getPdescriptor(){
		if (properties == null) {
			try {
				PropertyDescriptor[] props = {
					new PropertyDescriptor("cdtitle", CartLineItem.class),
					new PropertyDescriptor("id", CartLineItem.class),
					new PropertyDescriptor("price", CartLineItem.class)};
					properties = props;
			} catch (IntrospectionException ex) {
				return null;
			}
		}
		return properties;     } 
 
	// Event set information will be obtained from introspection.
	private static EventSetDescriptor[] eventSets = null;
	private static EventSetDescriptor[] getEdescriptor(){
 
		// Here you can add code for customizing the event sets array.
 
		return eventSets;     } 
 
	// Method information will be obtained from introspection.
	private static MethodDescriptor[] methods = null;
	private static MethodDescriptor[] getMdescriptor(){
 
		// Here you can add code for customizing the methods array.
 
		return methods;     } 
 
	private static int defaultPropertyIndex = -1; 
	private static int defaultEventIndex = -1; 
 
 
// Here you can add code for customizing the Superclass BeanInfo.
 
	/**
	* Gets the bean's <code>BeanDescriptor</code>s.
	*
	* @return BeanDescriptor describing the editable
	* properties of this bean.  May return null if the
	* information should be obtained by automatic analysis.
	*/
	public BeanDescriptor getBeanDescriptor() {
		return getBdescriptor();
	}
 
	/**
	* Gets the bean's <code>PropertyDescriptor</code>s.
	*
	* @return An array of PropertyDescriptors describing the editable
	* properties supported by this bean.  May return null if the
	* information should be obtained by automatic analysis.
	* <p>
	* If a property is indexed, then its entry in the result array will
	* belong to the IndexedPropertyDescriptor subclass of PropertyDescriptor.
	* A client of getPropertyDescriptors can use "instanceof" to check
	* if a given PropertyDescriptor is an IndexedPropertyDescriptor.
	*/
	public PropertyDescriptor[] getPropertyDescriptors() {
		return getPdescriptor();
	}
 
	/**
	* Gets the bean's <code>EventSetDescriptor</code>s.
	*
	* @return  An array of EventSetDescriptors describing the kinds of
	* events fired by this bean.  May return null if the information
	* should be obtained by automatic analysis.
	*/
	public EventSetDescriptor[] getEventSetDescriptors() {
		return getEdescriptor();
	}
 
	/**
	* Gets the bean's <code>MethodDescriptor</code>s.
	*
	* @return  An array of MethodDescriptors describing the methods
	* implemented by this bean.  May return null if the information
	* should be obtained by automatic analysis.
	*/
	public MethodDescriptor[] getMethodDescriptors() {
		return getMdescriptor();
	}
 
	/**
	* A bean may have a "default" property that is the property that will
	* mostly commonly be initially chosen for update by human's who are
	* customizing the bean.
	* @return  Index of default property in the PropertyDescriptor array
	* 		returned by getPropertyDescriptors.
	* <P>		Returns -1 if there is no default property.
	*/
	public int getDefaultPropertyIndex() {
		return defaultPropertyIndex;
	}
 
	/**
	* A bean may have a "default" event that is the event that will
	* mostly commonly be used by human's when using the bean.
	* @return Index of default event in the EventSetDescriptor array
	*		returned by getEventSetDescriptors.
	* <P>		Returns -1 if there is no default event.
	*/
	public int getDefaultEventIndex() {
		return defaultEventIndex;
	}
}
 


Cart Bean Source


package ShopCart;
 
import java.beans.*;
 
public class Cart extends Object implements java.io.Serializable {
 
	private static final String PROP_SAMPLE_PROPERTY = "SampleProperty";
 
	private String sampleProperty;
 
	private PropertyChangeSupport propertySupport;
 
	/** Holds value of property lineItems. */
	public java.util.Vector lineItems;
 
	/** Creates new Cart */
	public Cart() {
		propertySupport = new PropertyChangeSupport ( this );
		lineItems = new java.util.Vector();
	}
 
	public String getSampleProperty () {
		return sampleProperty;
	}
 
	public void setSampleProperty (String value) {
		String oldValue = sampleProperty;
		sampleProperty = value;
		propertySupport.firePropertyChange (PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
	}
 
	public void addPropertyChangeListener (PropertyChangeListener listener) {
		propertySupport.addPropertyChangeListener (listener);
	}
 
	public void removePropertyChangeListener (PropertyChangeListener listener){
		propertySupport.removePropertyChangeListener (listener);
	}
 
	/** Getter for property lineItems.
	* @return Value of property lineItems.
	*/
	public java.util.Vector getLineItems() {
		return this.lineItems;
	}
 
	/** Setter for property lineItems.
	* @param lineItems New value of property lineItems.
	*/
	public void setLineItems(java.util.Vector lineItems) {
		this.lineItems = lineItems;
	}
 
	/** 
	* Returns the element number of the item in the cartItems as specified by
	* the passed ID
	*/  
	public int findLineItem(int pID) {
		System.out.println("Entering Cart.findLineItem()");
		int cartSize = (lineItems == null) ? 0 : lineItems.size();
		int i ;
		for ( i = 0 ; i < cartSize ; i++ )
		{
			if  ( pID == ((CartLineItem)lineItems.elementAt(i)).getId() )
				break ;
		}
 
		if (i >= cartSize) {
			System.out.println("Couldn't find line item for ID: " + pID);
			return -1 ;
		}
		else
			return i ;
	}
 
	/** Removes a cartItem from the cartItems list.
	* @param pID ID of CartLineItem to remove.
	*/
	public void removeLineItem(int pID) {
		System.out.println("Entering cart.removeLineItem()");
		int i = findLineItem(pID);
		if (i != -1) lineItems.remove(i);
		System.out.println("Leaving cart.removeLineItem()");
	}
}
 


ShopCart.jsp Source


<%@page contentType="text/html"%>
<%@ page import="java.util.*, ShopCart.*" %>
 
<html>
<head><title>Shopping Cart</title></head>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<body>
<h1> Shopping Cart </h1>
 
<jsp:useBean id="myCart" scope="session" class="ShopCart.Cart" /> 
<%
	String myOperation = request.getParameter("operation");
	session.setAttribute("myLineItems", myCart.getLineItems());
 
	if (myOperation.equals("Add")) {
		CartLineItem lineItem = new CartLineItem();
		lineItem.setId(request.getParameter("cdId")); 
		lineItem.setCdtitle(request.getParameter("cdTitle"));
		lineItem.setPrice(request.getParameter("cdPrice"));
		myCart.lineItems.addElement(lineItem);
	}
	if (myOperation.equals("Delete")) {
		String s = request.getParameter("cdId");
		System.out.println(s);
		int idVal = Integer.parseInt(s);
		myCart.removeLineItem(idVal);
	}
	if (((Vector)session.getAttribute("myLineItems")).size() == 0) {
		%>
		<jsp:forward page="EmptyCart.jsp" />
		<%
	}
%>
 
<TABLE border=1>
<TR>
	<TH>ID</TH>
	<TH>CD Title</TH>
	<TH>Price</TH>
</TR>
<c:forEach var="item" items="${myLineItems}">
<TR>
	<TD><c:out value="${item.id}"/></TD>
	<TD><c:out value="${item.cdtitle}"/></TD>
	<TD><c:out value="${item.price}"/></TD>
 
	<TD>
		<form method=get action="ShopCart.jsp">
		<input type=hidden name=cdId value="<c:out value="${item.id}"/>">
		<input type=hidden name=cdTitle value="<c:out value="${item.cdtitle}"/>">
		<input type=hidden name=cdPrice value="<c:out value="${item.price}"/>">
		<input type=submit name=operation value=Delete>
		</form>
	</TD>
</TR>
</c:forEach>
 
</TABLE>
<p>
<form method=get action="ProductList.jsp">
	<input type=submit value="Resume Shopping">
</form>
<form method=get action="PlaceOrder.jsp">
	<input type=submit value="Place Order">
</form>
<form method=get action="CancelOrder.jsp">
	<input type=submit value="Cancel Order">
</form>
</p>
 
</body>
</html>
 


EmptyCart.jsp Source

<%@page contentType="text/html"%>
<HTML>
<head><title>Empty Cart</title></head>
<body>
	<h1> Empty Cart </h1>
	Your shopping cart is empty.
	<p>
	<form method=get action="ProductList.jsp">
		<input type=submit value="Resume Shopping">
	</form>
	</p>
</body>
</HTML>


PlaceOrder.jsp Source

<%@page contentType="text/html"%>
<html>
<head><title>Place Order</title></head>
<body>
	<h1> Place Order </h1>
	<%
	session.invalidate();
	%>
	Your order has been placed.  Thank you for shopping.
	<p>
	<form method=get action="ProductList.jsp">
		<input type=submit value="Resume Shopping">
	</form> 
	</p>
</body>
</html>


CancelOrder.jsp Source

<%@page contentType="text/html"%>
<html>
<head><title>Cancel Order</title></head>
<body>
	<h1> Cancel Order </h1>
	<%
	session.invalidate();
	%>
	Your order has been cancelled. Thank you for shopping.
	<p>
	<form method=get action="ProductList.jsp">
		<input type=submit value="Resume Shopping">
	</form>
	</p>
</body>
</html>