Uselesssss Uselesssss - 1 month ago 5
Java Question

Displaying array values in jsp

I have following two array in my code

List<Double> centralityList = (List<Double>) request
.getAttribute("centralityList");

List<String> labelList = (List<String>) request
.getAttribute("labelList");.


Now I have six string values and corresponding 6 double values of the string in these two array. My question is how to display them in tabular format in my JSP?Is this possible

For Example:

label list contains [a,b,c,d,e]

centrality list contains- [4,6,8,9,0]

So now I want to display output like:

label list centrality list

a 4
b 6
c 8.
. .


etc

Answer

Yes of course you can. You can use scriplets but they are not recommended. Instead use JSTL.

Try this out:

Have this at top of your JSP:

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

And code for displaying data

<c:forEach begin="0" end="${fn:length(centralityList) - 1}" var="index">
   <tr>
      <td><c:out value="${centralityList[index]}"/></td>
      <td><c:out value="${labelList[index]}"/></td>
   </tr>
</c:forEach>