1

In PHP Page, to print array, I use print_r(); In jsp, to print string, out.print() is used. for example :

<?php print_r($_POST); ?>

was is equal in jsp??? How can I print array values in jsp?

vsam
  • 533
  • 3
  • 23

4 Answers4

1

you can try

ObjectMapper mapper = new ObjectMapper();
 System.out.println( mapper.defaultPrettyPrintingWriter().writeValueAsString(myList) );

myList is your array varible .

or for post data try

Map<String, String[]> parameters = request.getParameterMap();
for(String parameter : parameters.keySet()) {
    if(parameter.toLowerCase().startsWith("your object name in html")) {
        String[] values = parameters.get(parameter);
        //your code here
    }
}

and import java.util.Map

N B
  • 391
  • 3
  • 12
0

Use a for loop

String[] colors = {"red", "green", "blue"};
   for (int i = 0; i < colors.length; i++) {
      out.print("<P>" + colors[i] + "</p>");
   }
twodayslate
  • 2,803
  • 3
  • 27
  • 43
0

You could to something like this :

<%
    out.println(StringUtils.join(variable,"<br />");
%>

If you actually want to print POST variables, follow this link :

Get all parameters from JSP page

Community
  • 1
  • 1
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
0

@todayslate gave a good answer, only that you have to know the length of the array for that. Now, of course that you can just check the length (by colors.length), but I find it more appealing to do:

String[] colors = {"red", "green", "blue"};
//suppose you have no idea how many colors are in the 
//array since you received it from another method or something
for (String color: colors) {
     out.print("<P>" + color + "</p>");
}
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129