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 Sun ONE Studio 5 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 Sun ONE Studio 5 Source Editor, all formatting is lost. To automatically reformat the code, put the cursor in the Source Editor and press Ctrl-Shift-F.



Solaris and Linux users are advised not to copy these files, because the Source Editor does not read the carriage returns at the ends of lines. To view source files, unzip the CDShopCart source zip file and then mount the unzipped directory in the IDE.


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" password="PBPUBLIC" />
 
<%--<sql:setDataSource var="productDS"
url="jdbc:oracle:thin:@hostname:port#:SID"
driver="oracle.jdbc.driver.OracleDriver"
user="userid" password="password" /> --%>
 
<%--<sql:setDataSource var="productDS"
url="jdbc:weblogic:mssqlserver4:database@hostname:port#"
driver="weblogic.jdbc.mssqlserver4.Driver"
user="userid" password="password" /> --%>
 
<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

/*
 * CartLineItem.java
 *
 * Created on April 11, 2003, 2:24 PM
 */
 
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 = 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 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);
        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.//GEN-FIRST:BeanDescriptor
    private static BeanDescriptor beanDescriptor = null;
    private static BeanDescriptor getBdescriptor(){
        //GEN-HEADEREND:BeanDescriptor
        
        // Here you can add code for customizing the BeanDescriptor.
        
        return beanDescriptor;     } //GEN-LAST:BeanDescriptor
    
    
    // Properties information will be obtained from introspection.//GEN-FIRST:Properties
    private static PropertyDescriptor[] properties = null;
    private static PropertyDescriptor[] getPdescriptor(){//GEN-HEADEREND:Properties
        
        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;     } //GEN-LAST:Properties
    
    // Event set information will be obtained from introspection.//GEN-FIRST:Events
    private static EventSetDescriptor[] eventSets = null;
    private static EventSetDescriptor[] getEdescriptor(){//GEN-HEADEREND:Events
        
        // Here you can add code for customizing the event sets array.
        
        return eventSets;     } //GEN-LAST:Events
    
    // Method information will be obtained from introspection.//GEN-FIRST:Methods
    private static MethodDescriptor[] methods = null;
    private static MethodDescriptor[] getMdescriptor(){//GEN-HEADEREND:Methods
        
        // Here you can add code for customizing the methods array.
        
        return methods;     } //GEN-LAST:Methods
    
    
    private static int defaultPropertyIndex = -1; //GEN-BEGIN:Idx
    private static int defaultEventIndex = -1; //GEN-END:Idx
    
    
    //GEN-FIRST:Superclass
    
    // Here you can add code for customizing the Superclass BeanInfo.
    
    //GEN-LAST:Superclass
    
    /**
     * 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

/*
 * Cart.java
 *
 * Created on April 11, 2003, 2:36 PM
 */
 
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;
    }
    
    public int findLineItem(int pID) {
        System.out.println("Entering Cart.findLineItem()");
        //Return the element number of the item in the cartItems
        //as specified by the passed ID.
        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;
    }
    
    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 the last item is removed from the cart...
if (((Vector)session.getAttribute("myLineItems")).size() == 0) {
//End scriptlet temporarily so that you can use the JSP
//?forward? tag to forward to the EmptyCart page.
%>
<jsp:forward page="EmptyCart.jsp" />
//Resume the scriptlet.
<%
}
%>
 
<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.id}"/>">
<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>
<!--Create the three buttons.-->
<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>
<!End the page.>
</body>
</html>


EmptyCart.jsp Source

<%@page contentType="text/html"%>
<html>
<head><title>Empty Cart</title></head>
<body>
<H1> Empty Cart </H1>
<!--Display a message-->
Your shopping cart is empty.
<P>
<!--Add a Resume Shopping button.-->
<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>
<!--Invalidate the session-->
<%
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>