Hai readers today i am going to give coding to create a simple shopping cart using java in jsp.This shopping cart is simple and get the values from an java file you can extend it to get the data from database file.<br>
Lets first create the basic design file store.jsp
Quote:<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="wxshop" uri="http://www.wrox.com/begjsp/eshop-functions-taglib" %>
<%@ page session="true" %>
<c:if test="${empty cats}">
<c:set var="cats" value="${wxshop:getCats()}" scope="application"/>
</c:if>
<html>
<head>
<title>S2Sgateway.com Shopping Mall</title>
<link rel=stylesheet type="text/css" href="store.css">
</head>
<body>
<table width="600">
<tr><td colspan="2" class="mainHead"><a href="http://s2sgateway.com">http://s2sgateway.com</a> Web Store</td></tr>
<tr>
<td width="20%">
<c:forEach var="curCat" items="${cats}">
<c:url value="/estore.jsp" var="localURL">
<c:param name="catid" value="${curCat.id}"/>
</c:url>
<a href="${localURL}" class="category">${curCat.name}</a>
</br>
</c:forEach>
</td>
<td width="*">
<h1></h1>
<table border="1" width="100%">
<tr><th align="left">Item</th><th align="left">Price</th><th align="left">Order</th></tr>
<c:set var="selectedCat" value="${param.catid}"/>
<c:if test="${empty selectedCat}">
<c:set var="selectedCat" value="1"/>
</c:if>
<c:forEach var="curItem" items="${wxshop:getItems(selectedCat)}">
<tr>
<td>${curItem.name}</td>
<td align="right">
<fmt:formatNumber value="${curItem.price / 100}" type="currency"/>
</td>
<td>
<c:url value="/shopcart.jsp" var="localURL">
<c:param name="action" value="buy"/>
<c:param name="sku" value="${curItem.sku}"/>
</c:url>
<a href="${localURL}"><b>BUY</b></a>
</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
</table>
</body>
</html>
[/css]
Then the processing file is shopcart.jsp where the selected items are displayed,where you can have the option to add the product,clear the product and continue shopping options the code<br>
[css htmlscript="true"]
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="wxshop" uri="http://www.wrox.com/begjsp/eshop-functions-taglib" %>
<%@ page session="true" %>
<c:set var="EXAMPLE" value="/example1"/>
<c:set var="SHOP_PAGE" value="/estore.jsp"/>
<c:set var="CART_PAGE" value="/shopcart.jsp"/>
<html>
<head>
<title>http://s2sgateway.com Web Store Shopping Cart</title>
<link rel=stylesheet type="text/css" href="store.css">
</head>
<body>
<c:if test="${!(empty param.sku)}">
<c:set var="prod" value="${wxshop:getItem(param.sku)}"/>
</c:if>
<jsp:useBean id="lineitems" class="java.util.ArrayList" scope="session"/>
<c:choose>
<c:when test="${param.action == 'clear'}">
${wxshop:clearList(lineitems)}
</c:when>
<c:when test="${param.action == 'inc' || param.action=='buy'}">
<c:set var="found" value="false"/>
<c:forEach var="curItem" items="${lineitems}">
<c:if test="${(curItem.sku) == (prod.sku)}">
<jsp:setProperty name="curItem" property="quantity"
value="${curItem.quantity + 1}"/>
<c:set var="found" value="true" />
</c:if>
</c:forEach>
<c:if test="${!found}">
<c:remove var="tmpitem"/>
<jsp:useBean id="tmpitem" class="com.wrox.begjsp.ch03.LineItem">
<jsp:setProperty name="tmpitem" property="quantity" value="1"/>
<jsp:setProperty name="tmpitem" property="sku" value="${prod.sku}"/>
<jsp:setProperty name="tmpitem" property="desc" value="${prod.name}"/>
<jsp:setProperty name="tmpitem" property="price" value="${prod.price}"/>
</jsp:useBean>
${wxshop:addList(lineitems, tmpitem)}
</c:if>
</c:when>
</c:choose>
<c:set var="total" value="0"/>
<table width="640">
<tr><td class="mainHead"><a href="http://s2sgateway.com">http://s2sgateway.com</a> Web Store</td></tr>
<tr>
<td>
<h1></h1>
<table border="0" width="640">
<tr><th colspan="5" class="shopCart">Your Shopping Cart</th></tr>
<tr><th align="left">Quantity</th><th align="left">Item</th><th align="right">Price</th>
<th align="right">Extended</th>
<th align="left">Add</th></tr>
<c:forEach var="curItem" items="${lineitems}">
<c:set var="extended" value="${curItem.quantity * curItem.price}"/>
<c:set var="total" value="${total + extended}"/>
<tr>
<td>${curItem.quantity}</td>
<td>${curItem.desc}</td>
<td align="right">
<fmt:formatNumber value="${curItem.price / 100}" type="currency"/>
</td>
<td align="right">
<fmt:formatNumber value="${extended / 100}" type="currency"/>
</td>
<td>
<c:url value="${CART_PAGE}" var="localURL">
<c:param name="action" value="inc"/>
<c:param name="sku" value="${curItem.sku}"/>
</c:url>
<a href="${localURL}"><b>Add 1</b></a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="5"> &nbsp;
</td>
</tr>
<tr>
<td colspan="3" align="right"><b>Total:</b></td>
<td align="right" class="grandTotal">
<fmt:formatNumber value="${total / 100}" type="currency"/>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="5">
<c:url value="${CART_PAGE}" var="localURL">
<c:param name="action" value="clear"/>
</c:url>
<a href="${localURL}">Clear the cart</a>
</td>
</tr>
<tr>
<td colspan="5">
<c:url value="${SHOP_PAGE}" var="localURL"/>
<a href="${localURL}">Return to Shopping</a>
</td>
</tr>
</table>
</td></tr>
</table>
</body>
</html>
[/css]
The java file where the datas we can get is as follows
[java]
import java.util.*;
public class EShop {
public EShop() {
}
public static ArrayList getCats() {
ArrayList values = new ArrayList();
values.add( new Category("1", "Systems"));
values.add( new Category("2", "Software"));
values.add( new Category("3", "Books"));
return values;
}
public static ArrayList getItems(String catid) {
ArrayList values = new ArrayList();
if (catid.equals("1")) {
values.add(new Product("232", "Pentium 4 - 4 GHz, 512 MB, 300 GB",
"", 98999));
values.add(new Product("238", "AMD Opteron - 4 GHz, 1 GB, 300 GB",
"", 120099));
} else if (catid.equals("2")) {
values.add(new Product("872", "Tomcat 5 Server for Windows",
"", 9900));
values.add(new Product("758", "Tomcat 5 Server for Linux",
"", 9900));
} else if (catid.equals("3")) {
values.add(new Product("511", "Beginning JavaServer Pages",
"", 3999));
values.add(new Product("188", "Professional Apache Tomcat 5",
"", 4999));
values.add(new Product("148", "Apache Tomcat Bible",
"", 4999));
}
return values;
}
public static Product getItem(String sku) {
ArrayList cats = getCats();
Product foundProd = null;
for (int i=0; i < cats.size(); i++) {
Category curCat = (Category) cats.get(i);
ArrayList items = getItems(curCat.getId());
for (int j=0; j < items.size(); j++) {
Product curProd = (Product) items.get(j);
if (curProd.getSku().equals(sku)) {
foundProd = curProd;
break;
}
}
if (foundProd != null)
break;
}
return foundProd;
}
}
Here values are added in arraylist for each category and it is retrieved, when the users click the option in main shopping
page.
The other three java files you needed one for the getter and setter methods for product,category and itemprice<br>
In order to view links, you must have to reply to this thread.
Just import the war file to your ide and run the program,if you need complete source file comment here.Happy coding