Scriptlet in JSP - access to request objects

I know that it is best to use jstl in JSPS, but I have been explicitly told to use scripts in this project. My question is that my servlet bound the Arraylist element to the request object, and I wanted to loop on that element with a script.

Example: my servlet attaches this and forwards it to jsp

 request.setAttribute("list", Content); where Content is Arraylist<String>

jsp should restore this object and print it on the page I tried:

  <%    
          ArrayList<String> cont =  (ArrayList)request.getAttribute("Content");
          for (int i=0;i<cont.size();i++)
          {
              out.println(cont.get(i));

          }
   %> 

Here is the error 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:    %> 
+5
source share
4 answers

Try iterating the Arraylistelements with Iterator.

out.printlnprints in the browser and System.out.println()displays on 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);
    }
   %> 
+4
source

Have you tried request.getAttribute?

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/>
0

ArrayList "0", for i

0

All Articles