shoppingcartupdate.jsp
来自「实现一个网络购物」· JSP 代码 · 共 68 行
JSP
68 行
<%
if(actionId.equals("fast")||actionId.equals("order")||actionId.equals("delete")||postingStr!=null) {
TreeMap products = (TreeMap) session.getAttribute("products");
if(products==null) {
products = new TreeMap();
try { session.setAttribute("products",products);
} catch(Exception e) { }
}
if((actionId.equals("order")||actionId.equals("fast"))&&(products.get(productId)==null)) { // *** add the product ***
products.put(productId,"1");
} else if(!postingStr.equals("")) { // *** update shoppingcart ***
postingStr += "|";
if(postingStr.indexOf("|valP")>-1) { // *** update the products in the session ***
int qpos = postingStr.indexOf("|valP");
while(qpos>-1) {
int vstart = postingStr.indexOf("=",qpos);
String product = postingStr.substring(qpos+5,vstart);
int vend = postingStr.indexOf("|",vstart+1);
try {
String value = "" + Integer.parseInt(postingStr.substring(vstart+1,vend));
products.put(product,value);
} catch(Exception e) { }
qpos = postingStr.indexOf("|valP",vend);
}
}
String memberId = getResponseVal("valM",postingStr);
if(!memberId.equals("")) { // *** add the memberid to the session ***
try { session.setAttribute("memberid",memberId);
} catch(Exception e) { }
}
String donationStr = getResponseVal("valD",postingStr);
if(!donationStr.equals("")) { // *** add the donation to the session ***
donationStr = donationStr.replace(',','.');
if(donationStr.indexOf(".")==-1) donationStr += ".00";
try {
donationStr = "" + (new Double(100*Double.parseDouble(donationStr))).intValue();
session.setAttribute("donation",donationStr);
} catch(Exception e) { }
}
int qpos = postingStr.indexOf("|q");
while(qpos>-1) { // *** add the answers the session ***
int vstart = postingStr.indexOf("=",qpos);
String question = postingStr.substring(qpos+2,vstart);
int vend = postingStr.indexOf("|",vstart+1);
String answer = postingStr.substring(vstart+1,vend);
if(answer!=null){
session.setAttribute("q"+ question,answer);
}
qpos = postingStr.indexOf("|q",vend);
}
if(actionId.equals("delete")) { // *** delete the product ***
products.remove(productId);
}
}
// *** count number of items ***
TreeMap productsIterator = (TreeMap) products.clone();
int totalItems = 0;
while(productsIterator.size()>0) {
String thisProduct = (String) productsIterator.firstKey();
totalItems += Integer.parseInt((String) productsIterator.get(thisProduct));
productsIterator.remove(thisProduct);
}
totalitemsId = "" + totalItems;
try { session.setAttribute("totalitems",totalitemsId);
} catch(Exception e) { }
}
%>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?