2013-05-06 87 views
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>"); 
    } 
    } 
%> 

的問題是,當我登錄,登錄會話屬性也進入購物車.........我知道問題出在哪裏,但無法解決......請幫助我。 Enumeration e = session.getAttributeNames();
while(e.hasMoreElements())...................這是主要問題所在......

+0

你可以顯示錯誤! – Ketan 2013-05-06 08:33:34

+0

登錄屬性,即「userid」被添加到購物車表中...... – 2013-05-06 08:47:08

回答

0

在遍歷時進行檢查通過枚舉:

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

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

這將跳過當前迭代,如果login屬性在枚舉發現開始下一次迭代。

+0

非常感謝先生,一個簡單的解決方案.....它讓我想起了一件事,只是堅持基本。非常感謝很多 – 2013-05-06 09:00:18

+0

不用擔心隊友,歡呼! :) – 2013-05-06 09:01:37