tags:

views:

42

answers:

2

For each loop is not working in opened popup while the same collection i can see when I write it on page

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>my cart</title>
</head>
<body>
<% ArrayList cart=(ArrayList)session.getAttribute("Cart"); 
out.println(cart);
//this line is working
%>
<table>
      <tr bgcolor="#EAEAFF">       
   <td><b>Product ID</b></td>   

  </tr>
  <c:forEach items="${cart}" var="current">
        <tr>
           <td><c:out value="${current}"/></td>            
        </tr>
      </c:forEach>      
      </table>
</body>
</html>
A: 
<c:forEach items="${sessionScope.cart}" var="current">

This should do the trick, you are trying to get a variable that is probably out of the default scope (page).

wtaniguchi
No, he has saved it with key `Cart` and is trying to access it with key `cart`. To fix it, he has to save it with key `cart` or to access it with key `Cart`. Besides, the `${}` just scans all scopes for the attribute, not only the page scope.
BalusC
+2  A: 

I see two problems with the code you posted:

1 - the case for cart is wrong. In the following code you must have Cart with a capital C:

<c:forEach items="${Cart}" var="current">

Also, this code

...
<% ArrayList cart=(ArrayList)session.getAttribute("Cart"); 
out.println(cart);
//this line is working
%>
...
<c:forEach items="${cart}" var="current">

does not pick up the cart variable you created with the scriptlet (if that's what you are thinking). cart is a local variable in the servlet generated from your JSP. The tag won't be able to access it).

2 - where is the taglib declaration for your c:forEach tag? Something like:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

If the server does not recognize the tag as what it is, it will output the thing directly into the response. Your browser won't recognize <c:forEach> and will ignore it (i.e. you have it in the source code but it is not displayed on the screen).

Use the following code for your JSP and it should work:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>my cart</title>
</head>
<body>
<table>
  <tr bgcolor="#EAEAFF">       
    <td><b>Product ID</b></td>   
  </tr>
  <c:forEach items="${Cart}" var="current">
    <tr>
     <td><c:out value="${current}" /></td>            
    </tr>
  </c:forEach>      
 </table>
</body>
</html>
dpb
thanks... it gave me much information about foreach loop.
Rozer