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.
I have looked at previous answers here and here, 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.
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].
"AccomSearch.java" Servlet
session.setAttribute("accomList",accomList);//set list as attribute
getServletConfig().getServletContext().getRequestDispatcher("/viewAccom.jsp").forward(request,response); // redirect to jsp page
^ Setting the "accomList" list to an attribute and redirecting the user to the jsp page.
"viewAccom.jsp" JSP Page
<%
List<Integer> accomList = new ArrayList<>();
accomList = (List<Integer>) request.getAttribute("accomList");
if (accomList==null){
out.println("accomList is null, Why is the list null?");
}else{
for (int i = 0; i < accomList.size(); i++) {
out.println(Integer.toString(accomList.get(i)));
}
}
%>
^ Attempting to retrieve the "accomList" attribute, cast it to a list, then display it.
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.
My guess is that I am either setting or retrieving the attribute incorrectly. Thanks in advance for any help you can spare.
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.