I know its best to use jstl in JSPS but I have been explicitly told to use scriptlets in this project.My question is that my servlet attached an item of Arraylist to the request object and i wanted to loop over that item using scriptlet.
Example : My servlet attaches this and forwards it to a jsp
request.setAttribute("list", Content); where Content is Arraylist<String>
The jsp is to retrieve this object and print it on the page which i tried is:
<%
ArrayList<String> cont = (ArrayList)request.getAttribute("Content");
for (int i=0;i<cont.size();i++)
{
out.println(cont.get(i));
}
%>
Here is the error that i get
org.apache.jasper.JasperException: An exception occurred processing JSP page /EnrolledSuccess.jsp at line 35
32: ArrayList<String> cont = (ArrayList)request.getAttribute("cont");
33: for (int i=0;i<=cont.size();i++)
34: {
35: out.println(cont.get(i));
36:
37: }
38: %>
Try iterating Arraylist
elements with Iterator
.
out.println
prints to the browser and System.out.println()
prints to the server console.
<%
ArrayList<String> cont = (ArrayList)request.getAttribute("list");
Iterator<String> itr = cont.iterator();
while (itr.hasNext()) {
String element = itr.next();
out.println(element);
}
%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With