Shopping Cart in JSP-3
The following is the code and form for addtocart page and basket page of the shopping cart.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="com.visualbuilder.shopping.*,java.util.*;" %>
<!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">
</head>
<body>
<form action="addtobasket.jsp" method="post">
<table> <tr><th>Product Code</th><th>Product Name</th><th>Product Price</th><th>Quantity</th></tr>
<% Basket basket = (Basket)session.getAttribute("basket");
HashMap<String,ProductBean> prodList= basket.getProdList();
Iterator it=prodList.keySet().iterator();
double totalCost=0.0;
while(it.hasNext()){
ProductBean bean = prodList.get(it.next());
totalCost=totalCost+bean.getQty()*bean.getPrice();
%>
<tr><td><%=bean.getProdCode() %></td><td><%=bean.getProdName() %></td><td><%=bean.getPrice() %></td><td><%=bean.getQty() %></td></tr>
<%} %>
<tr><td> </td><td> </td><td><b>Total</b></td><td><%=totalCost %></td></tr>
</table>
<a href="productpage.jsp">Go to Product Page</a><br>
</form>
</body>
</html>
|
Form :-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="com.visualbuilder.shopping.*,java.util.*;" %>
<!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">
</head>
<body>
<jsp:useBean id="basket" class="com.visualbuilder.shopping.Basket" scope="session"></jsp:useBean>
<%
basket.setSelectedProducts(request);
%>
Products Added succesfully in Basket.<br>
<a href="productpage.jsp">Go to Product Page</a><br>
<a href="basketpage.jsp">Go to Basket</a><br>
</body>
</html>
|
Form :-
