购物车与登录杯子

时间:2013-05-06 08:31:17

标签: jsp

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

 <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())...................这是主要问题所在......

1 个答案:

答案 0 :(得分:0)

在遍历枚举时进行检查:

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

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

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