title: How to display data of an ArrayList using useBean; no JSTL
body: <p>My servlet is building an ArrayList of a specific class (or a null ArrayList) and then setting it as an attribute in request and then forward to a JSP. In the JSP I am displaying the contents of that ArrayList using scriptlet. </p>
  
  <p>Now in the JSP I want to display the contents of that ArrayList using useBean technology. Is it possible? <strong><em>I already know how to do it using JSTL technology</em></strong>. But this question is about using only useBean technology i.e. no JSTL and of course no scriptlet. </p>
  
  <p>This is my class <strong>TestClass</strong>:</p>
  
  <pre><code>public class TestClass implements Serializable {
     private int id;
     private String description;
     public int getId() { return this.id; }
     public void setId(int id) { this.id = id; }
     public String getDescription() { return this.description; }
     public void setDescription(String description) { this.description = description; }
  }
  </code></pre>
  
  <p>This is how I populate the ArrayList in my Servlet <strong>TestClassServlet</strong>:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     List&lt;TestClass&gt; testClassList = null;
     TestClass testClass = null;
     String criteria = request.getParameter("criteria");
  
     if (criteria == null || criteria.trim().isEmpty()) {
        request.setAttribute("result", testClassList);
  
     } else if (criteria.trim().equalsIgnoreCase("ALL")) {
        testClassList = new ArrayList&lt;TestClass&gt;();
        testClass = new TestClass();
        testClass.setId(1);
        testClass.setDescription("Normal");
        testClassList.add(testClass);
        testClass = new TestClass();
        testClass.setId(2);
        testClass.setDescription("Priority");
        testClassList.add(testClass);
        request.setAttribute("result", testClassList);
  
     } else {
        request.setAttribute("result", testClassList);
  
     }
  
     request.getRequestDispatcher("test.jsp").forward(request, response);
  }
  </code></pre>
  
  <p>This is how I am displaying it in JSP through scriptlet:</p>
  
  <pre><code>&lt;%
  List&lt;TestClass&gt; testClassList = (List&lt;TestClass&gt;) request.getAttribute("result");
  if (testClassList == null) {
  %&gt;
      &lt;select id="select_testClassList" disabled="disabled"&gt;
      &lt;/select&gt;
  &lt;%  
  } else {
  %&gt;
      &lt;select id="select_testClassList"&gt;
          &lt;option value=""&gt;&lt;/option&gt;
  &lt;%  
      for (TestClass testClass : testClassList) {
  %&gt;
          &lt;option value="&lt;%=testClass.getId()%&gt;"&gt;&lt;%=testClass.getDescription()%&gt;&lt;/option&gt;
  &lt;%      
      }
  %&gt;
      &lt;/select&gt;
  &lt;%
  }
  %&gt;
  </code></pre>
  
  <p>I first tried the useBean with the class as java.util.List </p>
  
  <pre><code>&lt;jsp:useBean id="testClassList" class="java.util.List" scope="request"&gt;&lt;/jsp:useBean&gt;
  </code></pre>
  
  <p>But it did not even compile as I got this error message:</p>
  
  <blockquote>
    <p>Cannot instantiate the type List</p>
  </blockquote>
  
  <p>Fair enough as per specs of useBean technology. I change it to java.util.ArrayList with generics: </p>
  
  <pre><code>&lt;jsp:useBean id="testClassList" class="java.util.ArrayList&lt;TestClass&gt;" scope="request"&gt;&lt;/jsp:useBean&gt;
  </code></pre>
  
  <p>But then I got this error message:</p>
  
  <blockquote>
    <p>org.apache.jasper.JasperException: /test2.jsp(14,0) The value for the
    useBean class attribute java.util.ArrayList is invalid.</p>
  </blockquote>
  
  <p>I then change it to java.util.ArrayList without generics:</p>
  
  <pre><code>&lt;jsp:useBean id="testClassList" class="java.util.ArrayList" scope="request"&gt;&lt;/jsp:useBean&gt;
  </code></pre>
  
  <p>After that I dont get any error. </p>
  
  <p>Now I dont know how to loop through the ArrayList using useBean technology. I think I have to use <code>jsp:getProperty</code> but that will work on object of TestClass and my concern is how to get object of TestClass from the ArrayList?</p>
  
  <p>Thanks</p>