0

我正在使用以下代码为书籍创建一个会话属性并在单击购物车按钮时显示它

 <a href="ShoppingCart?bname=<%=bName%>&bprice=<%=bPrice%>"><input type="image" src="pics/buy-now.png" height=80px width=240px style="position: absolute; bottom: 30px; right: 150px;" /></a>


ShoppingCart.jsp

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    String bName= request.getParameter("bname");
    String bPrice= request.getParameter("bprice");
    HttpSession sess = request.getSession();
    sess.setAttribute(bName, bPrice);


request.getRequestDispatcher("paranormal.jsp").forward(request, response);


}

   CheckCart.jsp

<table  border="1" cellpadding="5" cellspacing="5">
  <tr><th>Title</th><th>Price</th><th>Quantity</th><th>Delivery time</th> 
    <th>Remove</th></tr>
  <% 
    session.setMaxInactiveInterval(1800); 
   Enumeration e = session.getAttributeNames();    
    {
   while(e.hasMoreElements())
   {
       %>
       <tr>
       <%
       String book_naam = (String)e.nextElement();
       String book_price = (String)session.getAttribute(book_naam);%>
       <td><%=book_naam %></td>
       <td><%=book_price %></td>
       <td><Select name="quantity">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </Select>
       </td>
       <td>2-3 working days</td>
  <td><input type="submit" value="remove"   onclick="window.document.location.href='remove.jsp?paramPrice=<%=book_price%>&paramName=<%=book_naam%>'"/></td>
  </tr>
       <%
       //out.print(book_naam+"="+book_price+"<br>");   
   }
    }
 %>

问题是当我登录时,登录会话属性也进入购物车............我知道问题出在哪里但无法解决......请帮帮我. 枚举 e = session.getAttributeNames();
{ while(e.hasMoreElements())............这是主要问题所在......

4

1 回答 1

0

在遍历枚举时进行检查:

String book_naam = (String)e.nextElement();

if(book_naam.equals("login"))
{
continue;
}

login如果在枚举中找到属性,这将跳过当前迭代并开始下一次迭代。

于 2013-05-06T08:43:44.957 回答