Shopping Cart in JSP-2
The below are the remaining code for the Basket.
Basket.java
package com.visualbuilder.shopping;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
public class Basket {
HashMap<String, ProductBean> prodList =new HashMap<String, ProductBean>();
String selectedProducts;
public String getSelectedProdcuts() {
return selectedProducts;
}
public void setSelectedProducts(HttpServletRequest request) {
String[] ar = request.getParameterValues("selectedProducts");
if(ar != null && ar.length >0){
HashMap<String, ProductBean> map= ProductUtilities.getProductsAsMap();
for(int i=0;i<ar.length;i++){
ProductBean bean =map.get(ar[i]);
if(bean != null){
ProductBean product= prodList.get(ar[i]);
if(product != null){
product.setQty(product.getQty()+1);
}else{
product=bean;
}
prodList.put(product.getProdCode(),product);
}
}
}
}
public HashMap<String, ProductBean> getProdList() {
return prodList;
}
public void setProdList(HashMap<String, ProductBean> prodList) {
this.prodList = prodList;
}
}
|
Productpage.jsp
<%@ 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 border="1" > <tr><th>Product Code</th><th>Product Name</th><th>Product Price</th><th> </th></tr>
<% List<ProductBean> produList= ProductUtilities.getProductsAsList();
for (int i=0;i<produList.size();i++){
ProductBean bean = produList.get(i);
%>
<tr><td><%=bean.getProdCode() %></td><td><%=bean.getProdName() %></td><td><%=bean.getPrice() %></td><td><input type="checkbox" name="selectedProducts" value="<%=bean.getProdCode()%>"></td></tr>
<%} %>
</table>
<input type="submit" value="AddToCart">
</form>
</body>
</html>
|
Product Form:-
