6

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:    %> 
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • Looked at it .. still getting problems – Rajeshwar Jun 04 '12 at 01:42
  • I'd also try to not ignore exceptions, but try to read and understand them. If you can't interpret them, then you should include it in your question, not ignore it as if it's decoration. – BalusC Jun 04 '12 at 01:44

4 Answers4

6

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);
    }
   %> 
Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
0

have you tried request.getAttribute?

FSP
  • 4,677
  • 2
  • 19
  • 19
0
first, you must get attribute from request.
<br/>
<%<br/>
  ArrayList<String> list = (ArrayList<String>)request.getAttribute("list");<br/>
    for(int i = 0; i < list.size(); i++){<br/>
        //you can print the value<br/>
        out.printLn(list.get(i));<br/>
    }<br/>
%><br/>
alony
  • 10,725
  • 3
  • 39
  • 46
Joe
  • 1
0

Index for ArrayList starts from "0", so in for loop the condition should be either i