<td><%= book.getTitle() %></td> <td align="center"><%= quantity %></td> <td class = "right"> <%= new DecimalFormat( "0.00" ).format( price ) %> </td> <td class = "bold right"> <%= new DecimalFormat( "0.00" ).format( subtotal ) %> </td>
</tr> <% } // end of while loop%> <%-- end scriptlet for literal XHTML and --%> <%-- display table row containing shopping cart total --%> <tr> <td colspan = "4" class = "bold right"><b>总计: </b> <%= new DecimalFormat( "0.00" ).format( total ) %> </td> </tr> </table><% // make current total a session attribute session.setAttribute( "total", new Double( total ) ); } // end of else%> <!-- link back to books.jsp to continue shopping -->
session里删除 session要重新set吗?
部分代码如下:
<%@ page language="java" session="true" pageEncoding="GBK"%>
<%@ page import="ch14.*,ch15.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<html>
<body>购物车商品<TABLE > <TR>
<TD vAlign=top width=468 height=400>
<!-- 显示内容开始 -->
<%-- start scriptlet to display shopping cart contents --%>
<jsp:useBean id="dao" class="ch14.TitleDaoImpl" scope="request"/>
<%
Map cart = (Map) session.getAttribute("cart");
double total = 0; if (cart == null || cart.size() == 0)
out.println("<p>购物车当前为空.</p>");
else { // 创建用于显示内容的变量
Set cartItems = cart.keySet();
//Iterator iterator = cartItems.iterator();
Object[] isbn = cartItems.toArray();
Titles book;
CartItem cartItem; int quantity;
double price, subtotal;
%>
<table cellSpacing=0 cellPadding=0
width=590 border=1>
<thead><tr align="center">
<th>书籍名称</th>
<th>数量</th>
<th>价格</th>
<th>小计</th>
<th>删除</th>
</tr></thead><% // continue scriptlet int i = 0;
while (i < isbn.length) { // 计算总和
cartItem = (CartItem) cart.get((String)isbn[i]);
book = cartItem.getTitles();
quantity = cartItem.getQuantity();
price = book.getPrice();
subtotal = quantity * price;
total += subtotal;
i++;%>
<tr>
<td><%= book.getTitle() %></td> <td align="center"><%= quantity %></td> <td class = "right">
<%=
new DecimalFormat( "0.00" ).format( price )
%>
</td> <td class = "bold right">
<%=
new DecimalFormat( "0.00" ).format( subtotal )
%>
</td>
</tr>
<%
} // end of while loop%> <%-- end scriptlet for literal XHTML and --%> <%-- display table row containing shopping cart total --%>
<tr>
<td colspan = "4" class = "bold right"><b>总计: </b>
<%= new DecimalFormat( "0.00" ).format( total ) %>
</td>
</tr>
</table><%
// make current total a session attribute
session.setAttribute( "total", new Double( total ) );
} // end of else%>
<!-- link back to books.jsp to continue shopping -->
<a href = "/javaEE/ch15/viewBook.jsp">继续购物</a>
<form method = "get" action = "/javaEE/ch15/order.html">
<input type = "submit" value = "结 账" />
</form><!-- 显示内容结束 -->
</TD></TR></TABLE>
</BODY></HTML>
cart.remove(key); 能行吗