title: Display a Class ArrayList on JSP page
body: <p>I am a beginner to Java.</p>
  
  <p>What I am trying to achieve is take an input of Product code from JSP (index.jsp) webpage, call a servlet (getDetails) and get description and balance on hand for multiple location, from a DB2-I-series sedrver</p>
  
  <p>In Servlet for description I am using Map, but to get balance of multiple location i am using a custom class ArrayList (displayOnHand).</p>
  
  <p>Data is getting properly captured in servlet page for balance at multiple location, but when I do a setAttribute in Servlet and getAttribute in JSP for the same, I am not able extract data as 1 field at a time.</p>
  
  <p>Can someone please help men.</p>
  
  <p>Following is my code</p>
  
  <pre><code>-------- getDetails Servlet ------------------
  
  
  public class getDetails extends HttpServlet {
  
    /**
     * Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt;
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        /* write your code here */
  
        Map &lt; String, String &gt; messages = new HashMap &lt; String, String &gt; ();
        ArrayList columnNames = new ArrayList();
        displayOnHand data = new displayOnHand();
        ArrayList &lt; displayOnHand &gt; dataList = new ArrayList &lt; displayOnHand &gt; ();
  
        String scanSKU = request.getParameter("SKUScan");
        if (scanSKU == null || scanSKU.trim().isEmpty()) {
          messages.put("scanSKU", "Please Enter SKU/UPC/EAN Code");
        }
  
        if (messages.isEmpty()) {
  
          String driver = getServletContext().getInitParameter("driver");
          String connectionString = getServletContext().getInitParameter("connectionString");
          String username = getServletContext().getInitParameter("username");
          String password = getServletContext().getInitParameter("password");
  
          try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(connectionString, username, password);
            Statement stmt = conn.createStatement();
            String rsstmt = "select IDESCR from mm510lib.invmst where inumbr =" + scanSKU;
            ResultSet rs = stmt.executeQuery(rsstmt);
  
            String SKUName = "";
            while (rs.next()) {
              SKUName = rs.getString(1);
            }
            //String SKUName = rs.getString(1);
            messages.put("SKUDescription", SKUName);
            stmt = conn.createStatement();
            rsstmt = "select istore, IBHAND from mm510lib.invbal where inumbr =" + scanSKU;
            rs = stmt.executeQuery(rsstmt);
  
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
  
            //  Get column names
            //for (int i = 1; i &lt;= columns; i++) {
            //    columnNames.add(md.getColumnName(i));
            // }
            //  Get row data
            //while (rs.next()) {
            //    ArrayList row = new ArrayList(columns);
            //    for (int i = 1; i &lt;= columns; i++) {
            //        row.add(rs.getObject(i));
            //    }
            //    data.add(row);
            //}
            while (rs.next()) {
              //displayOnHand data = new displayOnHand();
              data.setLocationCode(rs.getInt(1));
              data.setOnhand(rs.getInt(2));
              dataList.add(data);
            }
            rs.close();
            stmt.close();
            conn.close();
          } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(getDetails.class.getName()).log(Level.SEVERE, null, ex);
            messages.put("connection", "Connection to Database could not be established");
          }
  
          request.setAttribute("messages", messages);
          request.setAttribute("onHandClass", data);
          request.setAttribute("OnHandDetails", dataList);
          request.getRequestDispatcher("index.jsp").forward(request, response);
        }
      }
    }
  
  
  ---------------------------------------------------- index.jsp ----------------------------------------------------
  
  
  &lt;%@page import="java.util.List" %&gt;
    &lt;%@page import="java.sql.Array" %&gt;
      &lt;%@page import="java.util.Locale.Category" %&gt;
        &lt;%@page import="java.util.ArrayList" %&gt;
          &lt;%@page import="JavaPackages.*" %&gt;
            &lt;%@page contentType="text/html" pageEncoding="UTF-8" %&gt;
              &lt;!DOCTYPE html&gt;
              &lt;html&gt;
  
              &lt;head&gt;
                &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
                &lt;title&gt;Store MIS V0.1&lt;/title&gt;
                &lt;link href="css/index.css" rel="stylesheet" type="text/css" /&gt;
  
                &lt;script type='text/javascript'&gt;
                  function isNumber(evt) {
                    evt = (evt) ? evt : window.event; // for IE
                    var charCode = (evt.which) ? evt.which : evt.keyCode;
                    if (charCode &gt; 31 &amp;&amp; (charCode &lt; 48 || charCode &gt; 57)) {
                      return false;
                    }
                    return true;
                  }
                &lt;/script&gt;
              &lt;/head&gt;
  
              &lt;body&gt;
                &lt;div class="hypLogo"&gt;
                  &lt;img src="images/logo.jpg" alt="" /&gt;
                &lt;/div&gt;
                &lt;div class="container"&gt;
                  &lt;div class="pageHeader"&gt;
                    &lt;h1&gt;Store MIS&lt;/h1&gt;
                    &lt;form action="getDetails.do" method="POST"&gt;
                      &lt;p&gt;
                        &lt;input type="text" name="SKUScan" value="" placeholder="Scan SKU" onkeypress="return isNumber(event)" /&gt;
                      &lt;/p&gt;
  
                      &lt;span class="error"&gt;${messages.scanSKU}&lt;/span&gt;
                      &lt;p class="submit"&gt;
                        &lt;input type="submit" name="commit" value="Enter"&gt;
                      &lt;/p&gt;
                      &lt;span class="error"&gt;${messages.connection}&lt;/span&gt;
                      &lt;span class="error"&gt;${messages.SKUDescription}&lt;/span&gt;
                    &lt;/form&gt;
                  &lt;/div&gt;
                &lt;/div&gt;
                &lt;% out.println( "in Script"); if (request.getAttribute( "OnHandDetails") !=n ull) { // retrieve your list from the request, with casting ArrayList&lt;displayOnHand&gt;list = (ArrayList
                  &lt;displayOnHand&gt;) request.getAttribute("OnHandDetails"); displayOnHand tempData = new displayOnHand() ; out.println("Array Size" + list.size()); // print the information about every category of the list for (int i = 0; i
                    &lt; list.size(); i++) { tempData.equals(list.get(i));
                    } } %&gt;
              &lt;/body&gt;
  
              &lt;/html&gt;
  
  ------------- displayOnHand Class ---------------
  
  public class displayOnHand {
  
  private int storeNumber;
  private int onHandBal;
  
  public displayOnHand() {
  
  }
  
  public void setLocationCode(int strNum) {
      storeNumber = strNum;
  }
  
  public int getLocationCode() {
      return storeNumber;
  }
  
  public void setOnhand(int onHand) {
      onHandBal = onHand;
  }
  
  public int getOnHand() {
      return onHandBal;
  }
  }
  </code></pre>