Search code examples
javajspforeachjstlnested-loops

How foreach works for List of Lists in JSP


["A","B","C","D","E","F"] - ArrayList 01

[1,2,3,4,5,6] - ArrayList 02

[10,20,30,40,50,60] - ArrayList 03

[100,200,300,400,500,600] - ArrayList 04

Main_List - [["A","B","C","D","E","F"],[1,2,3,4,5,6],[10,20,30,40,50,60],[100,200,300,400,500,600]];

  • Main_List is combination of four Array-lists

How can I retreive Main_List in to a table which has four columns?

<c:forEach items="${List_parameter}" var="post" varStatus="theCount">
    <tbody>
        <c:forEach items="${post}" var="value" varStatus="cell">
            <tr>
                <td scope="row">${theCount.count}</td>
                <td>${value.get(0)}</td>
                <td>${value.get(1)}</td>
                <td>${value.get(2)}</td>
                <td>${value.get(3)}</td>
                <td>-</td>
            </tr>
        </c:forEach>
    </tbody>
</c:forEach>

Servlet passing the list to JSP

Scanner scanner  = new Scanner(Result);
        ControlData controlData = new ControlData();
        while(scanner.hasNextLine())  
        {  
            token1 = scanner.nextLine();
            Wtcs = controlData.CtrlWeight(token1);
            NC = controlData.NofConditions(token1);
            Ccspps = controlData.previousComplex(token1);

            cdLine.add(token1);
            wtc.add(Ccspps);
            ncc.add(NC);
            ccpps.add(Wtcs);

        }  

        List arr[]={cdLine,wtc,ncc,ccpps};   
        scanner.close();     //close the scanner  
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/Control_structures.jsp");
        request.setAttribute("Code_string", arr);
        dispatcher.forward(request, response);
    }

Solution

  • I got the answer for my question.

    In servlet

            Scanner scanner  = new Scanner(Result);
            ControlData controlData = new ControlData();
            List<List<Comparable>> p =new ArrayList<List<Comparable>>();
            while(scanner.hasNextLine())  
            {  
                token1 = scanner.nextLine();
                Wtcs = controlData.CtrlWeight(token1);
                NC = controlData.NofConditions(token1);
                Ccspps = controlData.previousComplex(token1);
                List<Comparable> c =new ArrayList<Comparable>();
                c.add(token1);
                c.add(Wtcs);
                c.add(NC);
                c.add(Ccspps);
                p.add(c);
            }  
            scanner.close();     //close the scanner  
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/jsp_page.jsp");
            request.setAttribute("Code_string", p);
            dispatcher.forward(request, response);
    

    Now I have a simple list to pass the JSP.

    <c:forEach items="${Code_string}" var="post" varStatus="theCount1">
            <tbody>
    
                <tr>
                    <td>${post[0]}</td>
                    <td>${post[1]}</td>
                    <td>${post[2]}</td>
                    <td>${post[3]}</td>
                </tr>
            </tbody>
    </c:forEach>