String bookName=request.getParameter("bookName");
String orderNum=request.getParameter("OrderNum");
session.setAttribute(bookName,orderNum);把书名和数量存在session里面
遍历session里面的内容
<tr>
<%
Enumeration en = session.getAttributeNames();
while(en.hasMoreElements())
{
String bookName = en.nextElement().toString();
String bookNum = (String)session.getAttribute(bookName);
%>
<td><%=bookName%></td>
<td><%=bookNum%></td>
</tr>
<%}%>
BUG:如果在相同的书名下存储不同的数量,则读数据 时只能读出最后存的数量,就是相同的名字用枚举读智能读一次
怎么样删除在session里面的存储的书: 得到书名,然后直接删除
String bookName=request.getParameter("bookName");
request.getSession.removeAttribute(bookNames);
如何解决上面的bug:保存会话
{
String bookName = request.getParameter("'bookName);
Int bookNum = (Integer.parseInt)request,getParameter("bookNum");
Int n = request.getParameter.("bookName");
If(n!==null)
bookNum +=n;
Session .setAttribute(bookName,bookNum);
然后输出Object bookNum = session.getAttribute(bookName);}
以下是实现代码:(receive.jsp)
<%
String bookName=request.getParameter("bookName");
Integer num= new Integer(request.getParameter("num"));
//查看相同书名是否已添加并且读出此书的数量
Integer n = (Integer)session.getAttribute(bookName);
if(n!=null)
num+=n;
session.setAttribute(bookName,num);
%>
读出时把数量转换以下类型:做如下修改
<tr>
<%
Enumeration en = session.getAttributeNames();
while(en.hasMoreElements())
{
String bookName = en.nextElement().toString();
Object num = session.getAttribute(bookName);
%>
<td><%=bookName%></td>
<td><%=num%></td>
</tr>
<%}%>
完善:
设置一个购物车存储商品,然后把购物车存在session里面
List buyCar = new ArrayList();
Map p = new HashMap();
P.put("bookName",bookName);
P.put("num",num);
buyCar.add(p);//购买商品存在购物车里面
session.setAttribute("buyCar",buyCar);//把购物车存在session里面
以下是完整代码;receive.jsp
String bookName = request.getParameter("bookName");
String num = request.getParameter("num");
//创建一个购物车
List buyCar =(List)session.getAttribute("buyCar");
if(buyCar== null) buyCar = new ArrayList();
Map p = new HashMap();
p.put("bookName",bookName);
p.put("num",num);
buyCar.add(p);
session.setAttribute("buyCar",buyCar);
以下是show.jsp
<%
List buyCar = (List)session.getAttribute("buyCar");//当初错在request.getAttributer("buyCar")
//不报错但是显示不出来
if(buyCar!=null){
for(int i=0;i<buyCar.size();i++)
{
Map p =(Map)buyCar.get(i);
%>
<tr>
<td><%=p.get("bookName")%></td>
<td><%=p.get("num")%></td>
</tr>
<%}}%>
删除所选 的图书:remove.jsp//
选择要删除的图书
<form action="bookRemove.jsp" name="name" method="post">
书名:
<select name="bookName">
<option value="大学英语"selected="selected">
大学外语
</option>
<option value="高等数学"selected="selected">
高等数学
</option>
<option value="JAVA"selected="selected">
JAVA
</option>
</select>
<br />
<input type="submit" value="删除" />
</form>
Bookremkove.jsp
//以下是分析
先得到要删除图书的名字bookName
String bookName = request.getParameter("bookName");
查看购物车
Liser buyCar = (List)session.getAttribute("buyiCar");
判断buyCar是否为空
If(buyCar!= null)
循环的到购物车里的map
Int index= -1;
for(int i=0 ;i<buyCar.size();i++)
Map p=(Map)buyCar.get(i);
判断是否是所要删除的图书
If(bookName.equals(p.get("bookName")))
Index = i;
Break//如果找到则跳出循环
If(index!=-1)
buyCar.remove(index);
session.setAttribute("buyCar");
具体实现代码:
<%
String bookName = request.getParameter("bookName");
List buyCar = (List)session.getAttribute("buyCar");//得到购物车
if(buyCar!=null)
{
int index = -1;
for(int i=0;i<buyCar.size();i++)
{
Map p = (Map)buyCar.get(i);
if(bookName.equals(p.get("bookName")))
{
index=i;
break;
}
}
if(index!=-1)//如果查得到
{
buyCar.remove(index);
}
session.setAttribute("buyCar",buyCar);
}
%>
出现的问题:购买相同的书不能实现的数量的相加,而是买两次书本
String bookName = request.getParameter("bookName");
Integer num = new Integer(request.getParameter("num"));
//查看是否有购物车
List buyCar =(List)session.getAttribute("buyCar");
if(buyCar==null)
//如果没有就new一个
buyCar = new ArrayList();
//查看购物车里是否购物信息
Map p=null;
for(int i=0;i<buyCar.size();i++)
{
Map map = (Map)buyCar.get(i);
if(map.get("bookName").equals(bookName))
p = map;
num+=((Integer)p.get("num")).intValue();
p.put("num",num);
}
if(p==null)
{
p=new HashMap();
p.put("bookName",bookName);
p.put("num",num);
buyCar.add(p);
session.setAttribute("buyCar",buyCar);
}