sadzag
sadzag

Reputation: 83

cannot display an arrayList in JSP

My bean is called Zone, i got all Zones from sql Server with Hibernate. in DAO.Class:

List zones = session.createQuery("FROM Zone").list();

i want to send this List from a servlet to a jsp. In servlet.class

List result = dao.getListZone();
request.setAttribute("userList",result);
 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/home.jsp");
dispatcher.forward(request,response);

in my jsp:

List viewList = new ArrayList();

if(request.getAttribute("userList")!=null && request.getAttribute("userList")!="")
List<Zone> userList =  (List)request.getSession().getAttribute("userList");
 itr = userList.iterator();

while(itr.hasNext())
{

    if(count%2==0)
    {
     bgcolor = "#C8E2D1";
    }
    else
    {

        bgcolor = "#EAF8EF";
    }

    viewList = (ArrayList)itr.next();
    int id = Integer.parseInt(viewList.get(0).toString());
    viewItr = viewList.iterator();
    %>

    <tr style="background-color:<%=bgcolor%>;">
    <%  
    while(viewItr.hasNext())
    {

        %>
        <td><%=viewItr.next()%></td>

it says that it cannot cast my bean Zone to arrayList. and it says that the line

 itr = userList.iterator();

make the error.

Thank you for your help.


I got this error: java.lang.ClassCastException: bean.Zone cannot be cast to java.util.ArrayList in the first line of the code below

viewList = (ArrayList<Zone>)itr.next();
        int id = Integer.parseInt(viewList.get(0).toString());
        viewItr = viewList.iterator();

Upvotes: 3

Views: 491

Answers (1)

Jigar Joshi
Jigar Joshi

Reputation: 240898

1 You are checking if that attribute exists in request and then you are reading it from session

2 you need to cast it like

List<Zone> userList =  (List<Zone>)request.getAttribute("userList");

3 You need to use JSTL to make the clear separation between view and logic


Also See

Upvotes: 3

Related Questions