title: Setting List as an attribute in servlet and retrieving in JSP
body: <p>I am attempting to set a list of integers to an attribute within a servlet, and then retrieve the list in the redirected JSP page.</p>
  
  <p>I have looked at previous answers <a href="https://stackoverflow.com/questions/16982516/passing-a-list-from-servlet-to-jsp" title="here">here</a> and <a href="https://stackoverflow.com/questions/19766963/passing-arraylist-from-servlet-to-jsp">here</a>, as well as others, and my code seems identical to the answers to me. Obviously I have done something wrong but I can't see the issue.</p>
  
  <p>Please note that the "accomList" is set correctly in the servlet as I have tested this by outputting the list to the console in the servlet. It is set to [1,2,3,4,21].</p>
  
  <p><strong>"AccomSearch.java" Servlet</strong></p>
  
  <pre><code>session.setAttribute("accomList",accomList);//set list as attribute
  getServletConfig().getServletContext().getRequestDispatcher("/viewAccom.jsp").forward(request,response); // redirect to jsp page
  </code></pre>
  
  <p>^ Setting the "accomList" list to an attribute and redirecting the user to the jsp page.</p>
  
  <p><strong>"viewAccom.jsp" JSP Page</strong></p>
  
  <pre><code> &lt;%
             List&lt;Integer&gt; accomList = new ArrayList&lt;&gt;();
             accomList = (List&lt;Integer&gt;) request.getAttribute("accomList");
             if (accomList==null){
                out.println("accomList is null, Why is the list null?");
             }else{
                for (int i = 0; i &lt; accomList.size(); i++) {
                     out.println(Integer.toString(accomList.get(i)));
                 }
             }
  
          %&gt;
  </code></pre>
  
  <p>^ Attempting to retrieve the "accomList" attribute, cast it to a list, then display it.</p>
  
  <p>When the code is run the "accomList" variable in the JSP page returns as "Null". So the String "accomList is null, Why is the list null?" is displayed in the browser. This is not intended.</p>
  
  <p>My guess is that I am either setting or retrieving the attribute incorrectly. Thanks in advance for any help you can spare. </p>
  
  <p>Also, This is my first stackOverflow question, so let me know if I have posted this in the wrong area, or the formatting is bad, etc.</p>