I am trying to pass ArrayList which contains object from servlet to JSP. But
Servlet file:
request.setAttribute("servletName", categoryList); //categorylist is an arraylist contains object of class category
getServletConfig().getServletContext().getRequestDispatcher("/GetCategory.jsp").forward(request,response);
JSP file:
//category class
<% Category category = new Category();
//creating arraylist object of type category class
ArrayList<Category> list = ArrayList<Category>();
//storing passed value from jsp
list = request.getAttribute("servletName");
for(int i = 0; i < list.size(); i++) {
category = list.get(i);
out.println( category.getId());
out.println(category.getName());
out.println(category.getMainCategoryId() );
}
%>
the possible errors would be...
1.you set the array list from the servelt in the session, not the in the request.
2.the array you set is null.
3.you redirect the page instead of forward it.
also you should not initialize the
list
and thecategory
in jsp. try this.here list attribute name set in request
request.setAttribute("List",list);
andArrayList list=new ArrayList();
In the servlet code, with the instruction
request.setAttribute("servletName", categoryList)
, you save your list in the request object, and use the name "servletName" for refering it.By the way, using then name "servletName" for a list is quite confusing, maybe it's better call it "list" or something similar:
request.setAttribute("list", categoryList)
Anyway, suppose you don't change your serlvet code, and store the list using the name "servletName". When you arrive to your JSP, it's necessary to retrieve the list from the request, and for that you just need the
request.getAttribute(...)
method.request.getAttribute("servletName")
method will returnObject
that you need to cast toArrayList