3

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

3 Answers 3

11

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>
Sign up to request clarification or add additional context in comments.

Comments

3

try this code

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

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

    String myString="";
%>
<table>
<tr><td>
<%

    for(int i = 0; i < labelList.size(); i++)
    {
       out.println((String)labelList.get(i));
    }

    %>
</td><td>
<%

    for(int i = 0; i < centralityList.size(); i++)
    {
       out.println((double)centralityList.get(i));
    }

    %>
</td>
</table>

You can achieve this eaisly by using JSTL which is even more easy and far better way but as in your code I didn't find any evidence of using JSTL , so this is the way for now

2 Comments

I think using JSTL tags for these tasks is the correct approach rather using scriplets and hardcoading the logic.I prefer @Abu approach. Am I correct..please give reply
yes sure.I also want to suggest you to use JSTL but as you didn't mention anywhere that you are using JSTL so I gave you this example.Thats why at the end of the answer I suggest you to use JSTL.
0

You can use JSTL tags and iterate through this-

<c:forEach var="Array" items="userNames">

         // do something with the element
</c:forEach>

Use this in your jsp page

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

2 Comments

use this value as items as per your array name - "centralityList".
Inside the foreach you can print like this - <tr> <td>${centrallylist}</td> </tr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.