求救!很急!JSP购物车统计商品数量的问题!

joe8282 joe8282 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
老师要我们做页面,好难,怎么搞都不行,大侠帮帮忙!
要求是这样的:修改下面的程序,实现统计购物车中每种商品的数量,在商品名称后面显示商品数量,不要重复显示商品!
代码如下:(代码如何修改请解答!)
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html;charset=GBK" %>
<%
String [] products = {"土豆","黄瓜","西红柿","茄子","海带"};
StringBuffer showProductList = new StringBuffer();
StringBuffer inCartProducts = new StringBuffer();
int i;
Vector inCart = new Vector();

if (session.getValue("cart")!=null)
inCart = (Vector)session.getValue("cart");

if (request.getParameter("add")!=null)
{
try{
i = Integer.parseInt(request.getParameter("item"));
inCart.addElement(new Integer(i));
}catch(NumberFormatException e){
}
}

if (request.getParameter("remove")!=null)
{
try{
i = Integer.parseInt(request.getParameter("item"));
inCart.removeElement(new Integer(i));
}catch(NumberFormatException e){
}
}

session.putValue("cart",inCart);

Integer [] s = new Integer[inCart.size()];
inCart.copyInto(s);

for (i=0; i<s.length; i++)
{
inCartProducts.append((i+1)+":"+products[s[i].intValue()]+"<br>\n");
}

for (i=0; i<products.length; i++)
{
showProductList.append("<option value=" + i + ">" + products[i] + "\n");
}
%>

<html>
<body>
<form method=POST action=cart.jsp>
<br>
<br><br>
<select Name="item">
<%=showProductList%>
</select>
<input Type=submit name="add" value="加入购物车">
<input Type=submit name="remove" value="从购物车删除">
</form>
<hr>
你的购物车中有下列商品:<br>
<%=inCartProducts%>
</body>
</html>
...全文
评论 13
收藏 已收藏
分享
返回顶部
joe8282
Web 开发 81,108 成员 341,721 内容