jsp 中session的购物车问题
buy1.html<%@pagelanguage="java"contentType="text/html;charset=gb2312"%><html><head><t...
buy1.html
<%@page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>购物网站</title>
</head>
<body>
<center>请选择要选购的商品</center>
<form action="display.jsp" method=post name=form>
<br><hr>
<input type="checkbox" name="item" value="0"> 篮球<br><br>
<input type="checkbox" name="item" value="1"> 足球<br><br>
<input type="checkbox" name="item" value="2"> 乒乓球<br><br>
<input type="checkbox" name="item" value="3"> 地球<br><br>
<p>
<input type="submit" value="加入购物车" name="submit">
</form>
<a href="buy2.html">查看其他商品<a>
</body>
</html>
buy2.html
<%@page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>购物网站</title>
</head>
<body>
<center>请选择要选购的商品</center>
<form action="display.jsp" method=post name=form>
<br><hr>
<input type="checkbox" name="item" value="4"> 电脑<br><br>
<input type="checkbox" name="item" value="5"> 电视机<br><br>
<input type="checkbox" name="item" value="6"> 宝马<br><br>
<input type="checkbox" name="item" value="7"> 法拉利<br><br>
<p>
<input type="submit" value="加入购物车" name="submit">
</form>
<a href="buy1.html">查看其他商品<a>
</body>
</html>
display.jsp
<%@page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>购物车</title>
</head>
<body>
<center>您选购的商品</center>
<br><hr>
<%
Integer itemCount=(Integer)session.getValue("itemCount");
if(itemCount==null){
itemCount=new Integer(0);
}
%>
<%
String[] item={"篮球","足球","乒乓球","地球","电脑","电视机","宝马","法拉利",};
String[] itemsSelected=request.getParameterValues("item");
String itemName="";
if(itemsSelected!=null){
for(int i=0;i<itemsSelected.length;i++){
itemName=itemsSelected[i];
itemCount=new Integer(itemCount.intValue()+1);
session.setAttribute("item"+itemCount,itemName);
session.setAttribute("itemCount",itemCount);
}
}
%>
<%
for(int i=0;i<itemCount.intValue();i++){
String items=(String) session.getValue("item"+i);
int ite=0;
ite=Integer.parseInt(items);
out.println(item[ite]+"<br>");
}
%>
</body>
</html>
请高手帮忙改正。 展开
<%@page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>购物网站</title>
</head>
<body>
<center>请选择要选购的商品</center>
<form action="display.jsp" method=post name=form>
<br><hr>
<input type="checkbox" name="item" value="0"> 篮球<br><br>
<input type="checkbox" name="item" value="1"> 足球<br><br>
<input type="checkbox" name="item" value="2"> 乒乓球<br><br>
<input type="checkbox" name="item" value="3"> 地球<br><br>
<p>
<input type="submit" value="加入购物车" name="submit">
</form>
<a href="buy2.html">查看其他商品<a>
</body>
</html>
buy2.html
<%@page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>购物网站</title>
</head>
<body>
<center>请选择要选购的商品</center>
<form action="display.jsp" method=post name=form>
<br><hr>
<input type="checkbox" name="item" value="4"> 电脑<br><br>
<input type="checkbox" name="item" value="5"> 电视机<br><br>
<input type="checkbox" name="item" value="6"> 宝马<br><br>
<input type="checkbox" name="item" value="7"> 法拉利<br><br>
<p>
<input type="submit" value="加入购物车" name="submit">
</form>
<a href="buy1.html">查看其他商品<a>
</body>
</html>
display.jsp
<%@page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>购物车</title>
</head>
<body>
<center>您选购的商品</center>
<br><hr>
<%
Integer itemCount=(Integer)session.getValue("itemCount");
if(itemCount==null){
itemCount=new Integer(0);
}
%>
<%
String[] item={"篮球","足球","乒乓球","地球","电脑","电视机","宝马","法拉利",};
String[] itemsSelected=request.getParameterValues("item");
String itemName="";
if(itemsSelected!=null){
for(int i=0;i<itemsSelected.length;i++){
itemName=itemsSelected[i];
itemCount=new Integer(itemCount.intValue()+1);
session.setAttribute("item"+itemCount,itemName);
session.setAttribute("itemCount",itemCount);
}
}
%>
<%
for(int i=0;i<itemCount.intValue();i++){
String items=(String) session.getValue("item"+i);
int ite=0;
ite=Integer.parseInt(items);
out.println(item[ite]+"<br>");
}
%>
</body>
</html>
请高手帮忙改正。 展开
展开全部
display.jsp 帮你修改了一下,有个小错误,见注释:
<%@page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>购物车</title>
</head>
<body>
<center>
您选购的商品
</center>
<br>
<hr>
<%
Integer itemCount = (Integer) session.getAttribute("itemCount");
if (itemCount == null) {
itemCount = new Integer(0);
}
%>
<%
String[] item = { "篮球", "足球", "乒乓球", "地球", "电脑", "电视机", "宝马",
"法拉利", };
String[] itemsSelected = request.getParameterValues("item");
String itemName = "";
if (itemsSelected != null) {
for (int i = 0; i < itemsSelected.length; i++) {
itemName = itemsSelected[i];
session.setAttribute("item" + itemCount, itemName);
session.setAttribute("itemCount", itemCount);
//itemCount++这句应该放在这里,不然session.getAttribute("item0")==null
//你对比一下原来的代码就知道为什么getAttribute("item0")是null了
itemCount = new Integer(itemCount.intValue() + 1);
}
}
%>
<%
for (int i = 0; i < itemCount.intValue(); i++) {
String items = (String) session.getAttribute("item" + i);
int ite = 0;
out.println(items + " ");
ite = Integer.parseInt(items);
out.println(item[ite] + "<br>");
}
%>
</body>
</html>
<%@page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>购物车</title>
</head>
<body>
<center>
您选购的商品
</center>
<br>
<hr>
<%
Integer itemCount = (Integer) session.getAttribute("itemCount");
if (itemCount == null) {
itemCount = new Integer(0);
}
%>
<%
String[] item = { "篮球", "足球", "乒乓球", "地球", "电脑", "电视机", "宝马",
"法拉利", };
String[] itemsSelected = request.getParameterValues("item");
String itemName = "";
if (itemsSelected != null) {
for (int i = 0; i < itemsSelected.length; i++) {
itemName = itemsSelected[i];
session.setAttribute("item" + itemCount, itemName);
session.setAttribute("itemCount", itemCount);
//itemCount++这句应该放在这里,不然session.getAttribute("item0")==null
//你对比一下原来的代码就知道为什么getAttribute("item0")是null了
itemCount = new Integer(itemCount.intValue() + 1);
}
}
%>
<%
for (int i = 0; i < itemCount.intValue(); i++) {
String items = (String) session.getAttribute("item" + i);
int ite = 0;
out.println(items + " ");
ite = Integer.parseInt(items);
out.println(item[ite] + "<br>");
}
%>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询