Enter your email address:

Delivered by FeedBurner


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
shopping cart in jsp&java
11-28-2011, 03:44 PM (This post was last modified: 01-14-2012 10:08 AM by mjm.)
Post: #1
shopping cart in jsp&java
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"> &amp;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>&amp;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
Find all posts by this user
Add Thank You Quote this message in a reply
[-] The following 1 user says Thank You to admin for this post:
mjm (01-21-2012)
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
01-14-2012, 10:29 AM
Post: #2
RE: shopping cart in jsp&java
Downloads are checked and working correctly
In order to view links, you must have to reply to this thread.
In order to view links, you must have to reply to this thread.

Note:
For all the jobs, make sure to call the given number and attend the walkin. if you get this information is not accurate, please reply to this thread to help others.
Find all posts by this user
Add Thank You Quote this message in a reply
01-14-2012, 12:45 PM
Post: #3
RE: shopping cart in jsp&java
project is very nice but pls provide us the whole code.
Find all posts by this user
Add Thank You Quote this message in a reply
01-15-2012, 01:27 PM
Post: #4
RE: shopping cart in jsp&java
Can any one provide me tutorials and example on generics collection.
Find all posts by this user
Add Thank You Quote this message in a reply
01-21-2012, 11:28 PM
Post: #5
RE: shopping cart in jsp&java
(01-14-2012 12:45 PM)mukesh1234 Wrote: In order to view links, you must have to reply to this thread.project is very nice but pls provide us the whole code.
I think those code will work fine.

(01-15-2012 01:27 PM)mukesh1234 Wrote: In order to view links, you must have to reply to this thread.Can any one provide me tutorials and example on generics collection.

We will try, if you find any code, please do share.
In order to view links, you must have to reply to this thread.
In order to view links, you must have to reply to this thread.

Note:
For all the jobs, make sure to call the given number and attend the walkin. if you get this information is not accurate, please reply to this thread to help others.
Find all posts by this user
Add Thank You Quote this message in a reply
03-14-2012, 04:25 AM
Post: #6
RE: shopping cart in jsp&java
Interesting tutorial
Find all posts by this user
Add Thank You Quote this message in a reply
04-13-2012, 08:51 PM
Post: #7
RE: shopping cart in jsp&java
thank u
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Possibly Related Threads...
Thread: Author Replies Views: Last Post
Tongue Students management java project download rajasri 25 2,290 05-15-2012 12:45 PM
Last Post: itischinmay
Sad java.sql.SQLException: General error solution rajasri 3 1,090 02-07-2012 03:05 PM
Last Post: shahbaz07dbit
Sad create a file from form input in java,jsp rajasri 0 258 12-19-2011 06:32 PM
Last Post: rajasri
Wink Reading Html code of all the url using java rajasri 0 187 12-19-2011 06:17 PM
Last Post: rajasri
Star pagination in jsp,java rajasri 0 238 12-19-2011 05:32 PM
Last Post: rajasri
Music How to extract war file in java rajasri 0 2,887 12-14-2011 12:07 PM
Last Post: rajasri
Thumbs Up Solution for java.lang.NoClassDefFoundError: javax/transaction/Synchronization rajasri 0 1,013 12-14-2011 12:00 PM
Last Post: rajasri
Video How to use batch update in java and jsp rajasri 0 471 12-12-2011 11:38 PM
Last Post: rajasri
Bug Print current time using java in jsp page rajasri 0 620 12-12-2011 11:20 PM
Last Post: rajasri
Tongue Send Email in Java rajasri 0 935 12-12-2011 10:27 PM
Last Post: rajasri

Forum Jump:


User(s) browsing this thread: 1 Guest(s)