0

I'm trying to send a list as parameter to a JSP file where i want it showed. My JSP file is called showList.jsp.

Have created a servlet which i use to control everything in my program. When a user click on a button "show List" some code will be performed, and this code will print out the list:

my code:

if (session.getAttribute("show") == "show") {

        List opr = null;
        try {

            opr  = func.showlist(opr);
            for(int i = 0; i < opr.size(); i++){
                System.out.println(opr.get(i));
            }

        } catch (Exception e) {

            System.out.print("Error found, when trying to show list");

        }

        session.removeAttribute("administrator");
        session.removeAttribute("show");
        session.removeAttribute("create");
        return;

    }

Here you can see it just prints what's in my arraylist (size).

Updated code:

  List opr = null;
        try {

            opr  = func.showlist(opr);
            for(int i = 0; i < opr.size(); i++){
                System.out.println(opr.get(i));

            }
            request.setAttribute("list", opr);

        } catch (Exception e) {

            System.out.print("Error found, when trying to show list");

        }

        session.removeAttribute("administrator");
        session.removeAttribute("show");
        getServletConfig().getServletContext().getRequestDispatcher("/getList.jsp");

    }
Raaydk
  • 147
  • 2
  • 12

1 Answers1

1

Try with JSP JSTL to iterate a list in JSP

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

<c:if test="${not empty list}">
    <table>
        <c:forEach var="x" varStatus="status" items="${list}">
            <tr>
                <td><c:out value="${x}" /></td>
            </tr>
        </c:forEach>
    </table>
</c:if>

Simply set the data as request attribute using ServletRequest#setAttribute().

For more samples have a look at below posts:

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76