This Servlet, I do GROUP BY on color car. Inserted into the request and advertise in jsp
but it does not convert.
Query query = session.createQuery("select count(carColor), carColor from Cars group by carColor order by carColor");
List<Cars> list = query.list();
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object[] obj = (Object[]) iter.next();
System.out.println(obj[0] + " " + obj[1]);
}
request.setAttribute("list", list);
RequestDispatcher rd = request.getRequestDispatcher("test.jsp");
rd.forward(request, response);
Console:
2 White
10 Black
5 Blue
JSP:
[[Ljava.lang.Object;@1f3b536, [Ljava.lang.Object;@fdffb1,]]
Your code really makes no sense:
In the above line, you declare that your list is a list of Cars (which it is not)
Then you iterate on the list, and cast each element to
Object[]. How could a Cars instance ever be anObject[]? The list should be declared asList<Object[]>, and you shouldn’t use raw types. Your loop should be written asOr, even simpler:
Now, in the JSP, I suspect you’re just using
${list}to display the list. This simply calls the toString() method on the list, which itself calls the toString() method of each element. Since each element is anObject[], the result string is[Ljava.lang.Object;@1f3b536, which means “array of Object with hashCode 1f3b536”.To display the elements of the list, you should iterate over the list, as you must do it in the Java code: