addtocart.jsp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<%--
	This page will handle only AJAX requests
--%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*, cartexample.*"%>
<%@ page errorPage="ajaxerror.jsp"%>

<%
String quantity = request.getParameter("quantity");
String code = request.getParameter("code");
String name = request.getParameter("name");
String price = request.getParameter("price");
String image = request.getParameter("image");
int quantityNum = 0;
double priceNum = 0;

try {
	
	if(request.getHeader("X-Request-page") == null || !request.getHeader("X-Request-page").equals( "my_ajax_script" ) ) {
		throw new Exception("Η σελίδα δεν επιτρέπει την απευθείας κλίση");
	}
	
	if( quantity == null || quantity.length() == 0
		|| code == null || code.length() == 0
		|| name == null || name.length() == 0
		|| price == null || price.length() == 0
		|| image == null || image.length() == 0 ) {
		
		throw new Exception("Μη έγκυρο αίτημα");	
	}	
	
} catch(Exception e) {
	throw new Exception(e.getMessage() );	
}

try {
	quantityNum = Integer.parseInt(quantity);
	
	if(quantityNum <= 0) {
		throw new Exception();
	}
	
} catch(Exception e) {
	throw new Exception( "Η ποσότητα '" + quantity + "' δεν είναι έγκυρη" );	
}

try {
	priceNum = Double.parseDouble(price);
	
	if(priceNum <= 0) {
		throw new Exception();
	}
	
} catch(Exception e) {
	throw new Exception( "Η τιμή '" + price + "' δεν είναι έγκυρη" );	
}


List<Product> productlist = null;
int totalitems = 0;

productlist = (List)session.getAttribute("cart");

Product product = new Product();
product.setCode(code);
product.setName(name);
product.setPrice(priceNum);
product.setQuantity(quantityNum);
product.setImage(image);

if(productlist == null) {
	
	productlist = new ArrayList<Product>();
	productlist.add(product);
	session.setAttribute("cart", productlist);	
	totalitems = getTotalItems(productlist);	
	session.setAttribute("cartitems", totalitems );
	
	out.println( totalitems );
	return;
	
} 	
		
List<Product> newproductlist = new ArrayList<Product>();
boolean isAlreadyinCart = false;

for (int i = 0; i < productlist.size(); i++) {
	Product tmpproduct = productlist.get(i);
	int num1 = tmpproduct.getQuantity();
	int num2 = product.getQuantity();
	
	if( tmpproduct.getCode().equals(product.getCode()) ) {
		tmpproduct.setQuantity( num1 + num2 );
		
		newproductlist.add(tmpproduct);				
		isAlreadyinCart = true;
	} else {
		newproductlist.add(tmpproduct);
	} 		
}

if(!isAlreadyinCart) {
	newproductlist.add(product);	
}

session.removeAttribute("cart");
session.setAttribute("cart", newproductlist);
totalitems = getTotalItems(newproductlist);
session.setAttribute("cartitems", totalitems);

out.println( totalitems );
%>

<%!
private int getTotalItems(List<Product> items) {
	
	int sum = 0;
	
	for(Product product: items) {		
		sum += product.getQuantity();		
	}
	
	return sum;
}
%>