-1

I have a String activity[]=new String[2] say activity[0]=["sleeping"] and activity[1]=["eat","play"] and another String name[]=new String[2] say name[0]="amit" and name[1]="sumit" I have passed both string array to jsp page. Now I want to display these two strings in tabular manner say

enter image description here

how can I achieve it using jsp.

6
  • In jsp use table tag,name and hobby are th Commented Nov 13, 2013 at 11:31
  • 3
    How does activity[1] have two values? Commented Nov 13, 2013 at 11:32
  • @javaBeginner I want to know how it can be achieved using for loop <c:forEach></<c:forEach> Commented Nov 13, 2013 at 11:32
  • @peeskillet activity was List<String> which converted to string[] Commented Nov 13, 2013 at 11:34
  • Take a look at this: stackoverflow.com/questions/15697456/… Also, consider handling a Map, instead of separate collections. Commented Nov 13, 2013 at 11:42

1 Answer 1

1

In .jsp file do like this:

<%
        String[][] activityList=(String[][])request.getAttribute("activity");
        String[] nameList=(String[])request.getAttribute("name");

        out.print("<table border='1'>");
        out.print("<tr>");
        out.print("<td>Name</td>");
        out.print("<td>Hobby</td>");
        out.print("</tr>");
        for(int i=0;i<activityList.length;i++){
            String [] a=activityList[i];
            for(String h:a){
                out.print("<tr>");
                out.print("<td>"+nameList[i]+"</td>");
                out.print("<td>"+h+"</td>");
                out.print("</tr>");
            }
        }
        out.print("</table>");
    %>
Sign up to request clarification or add additional context in comments.

Comments

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.