日期:2014-05-18  浏览次数:20831 次

jsp session持久化遇到怪问题
问题描述

购物车,新加物品时,修改数量没问题。退出登陆。重新登陆,购物车从数据库读取数据,此时修改数量就出现怪问题,明明我修改1,但是1没变,2变了。

<%@   page   language= "java "   import= "java.util.* "   pageEncoding= "GB18030 "%>
<%@   page   import= "com.hotmail.rayyu8.* "%>
<%
String   path   =   request.getContextPath();
String   basePath   =   request.getScheme()   +   ":// "
+   request.getServerName()   +   ": "   +   request.getServerPort()
+   path   +   "/ ";
Object   object=session.getAttribute( "usr ");
if(object==null){
response.sendRedirect( "login.jsp ");
}else{
//Collection   bookCols   =   (Collection)   session.getAttribute( "books ");
CartBean   shopCart   =   (CartBean)   session.getAttribute( "ShopCart ");

//获得参数列表的枚举,如果重新设置过数量的话,枚举便有值
Enumeration   paraNames   =   request.getParameterNames();
//根据枚举,重新设置购物车内商品数量
while   (paraNames.hasMoreElements())   {

String   bookIdStr   =   String.valueOf(paraNames.nextElement());
String   quantityStr   =   request.getParameter(bookIdStr);
//request.removeAttribute(bookIdStr);

if   (bookIdStr.equals( "submit ")){
break;
}
int   bookId   =   Integer.parseInt(bookIdStr);
int   quantity   =   Integer.parseInt(quantityStr);

shopCart.setItemNum(bookId,   quantity);
}

Collection   books   =   shopCart.getItems();


%>


<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN ">
<html>
<head>
<base   href= " <%=basePath%> ">

<title> 查看购物车 </title>

<meta   http-equiv= "pragma "   content= "no-cache ">
<meta   http-equiv= "cache-control "   content= "no-cache ">
<meta   http-equiv= "expires "   content= "0 ">
<meta   http-equiv= "keywords "   content= "keyword1,keyword2,keyword3 ">
<meta   http-equiv= "description "   content= "This   is   my   page ">
<!--
<link   rel= "stylesheet "   type= "text/css "   href= "styles.css ">
-->

</head>

<body> <center>
<P>
<form   action= "showcart.jsp "   method= "POST ">
<table   width= "800px "   border= "1 "   align= "center ">
<tr>
<td> 书名 </td>
<td> 价格 </td>
<td> 数量 </td>
<td> 小计 </td>
<td> 取消 </td>
</tr>
<%
float   total=0.0f;
for   (Object   obj   :   books)   {
CartItemBean   cartItem   =   (CartItemBean)   obj;
BookBean   book   =   cartItem.getBook();
total+=cartItem.getItemPrice();
%>
<tr>
<td>