how to send a complex object to a jsp

296 views Asked by At

My java classes are :

public class Name{

  private String name;
  private ArrayList<Phone> phoneList;

  // plus getters and setters
}

public class Phone{

      private String phone;

      // plus getters and setters
}

Supose i have an ArrayList of Name, like:

ArrayList<Name> nameList = some method that returns an ArrayList<Name>

My servlet code would be:

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  ArrayList<Name> nameList = some method that returns an ArrayList<Name>

  request.setAttribute("nameList", nameList);
  RequestDispatcher rd = request.getRequestDispatcher("someJSP.jsp");
  rd.forward(request, response);
}

my jsp code would be:

<c:choose>
  <c:when test="${not empty requestScope['nameList'] }">
    <c:forEach var="name" items="${requestScope[' nameList '] }">
      Name:${name.name }
        <c:choose>
          <c:when test="${not empty requestScope[' nameList.phoneList'] }">
            <c:forEach var="phone" items="${requestScope ['nameList.phoneList'] }">
              Phone:${phone.phone } </div>
            </c:forEach>
          </c:when>
          <c:otherwise>
         <span>The phone list is empty.</span>
          </c:otherwise>
      </c:choose>
    </c:forEach>
  </c:when>
</c:choose>

When i run the jsp file the nameList is not empty but the phone list is empty.

Somehow the phone list is lost and i don't know how to fix this.

2

There are 2 answers

1
Santhosh On

When i run the jsp file the nameList is not empty but the pone list is empty.,

Because you didnt set the value for the ponelist

Somehow the phone list is lost

It is not lost you dont have any method in the servlet to return the value from the getters and setters,

  ArrayList<Name> nameList = some method that returns an ArrayList<Name>    
  request.setAttribute("nameList", nameList);
  RequestDispatcher rd = request.getRequestDispatcher("someJSP.jsp");
  rd.forward(request, response);

So need to have another list and set the attribute for the class Phone like the same above,

  ArrayList<Phone> poneList = some method that returns an ArrayList<Phone>
  request.setAttribute("ponelist", ponelist);

Hope this helps !!

0
Martin On

i've found the solution.

The problem was in the condition of the second c:when and c:foreach. I was doing this:

<c:choose>
  <c:when test="${not empty requestScope['nameList'] }">
    <c:forEach var="name" items="${requestScope[' nameList '] }">
      Name:${name.name }
        <c:choose>
          <c:when test="${not empty requestScope[' nameList.phoneList'] }">
            <c:forEach var="phone" items="${requestScope ['nameList.phoneList'] }">
              Phone:${phone.phone } </div>
            </c:forEach>
          </c:when>
          <c:otherwise>
         <span>The phone list is empty.</span>
          </c:otherwise>
      </c:choose>
    </c:forEach>
  </c:when>
</c:choose>

And i replace them for this:

<c:choose>
  <c:when test="${not empty requestScope['nameList'] }">
    <c:forEach var="name" items="${requestScope[' nameList '] }">
      Name:${name.name }
        <c:choose>
          <c:when test="${not empty nameList.phoneList }">
            <c:forEach var="phone" items="${nameList.phoneList }">
              Phone:${phone.phone } </div>
            </c:forEach>
          </c:when>
          <c:otherwise>
         <span>The phone list is empty.</span>
          </c:otherwise>
      </c:choose>
    </c:forEach>
  </c:when>
</c:choose>

The phoneList is in every name object of the nameList that I have established in the request. When i loop the nameList i can access to the phoneList using the expression:

${nameList.phoneList}

I hope my English is correct.