0

I am attempting to send an ArrayList from a servlet to a JSP page. In the Servlet:

List<ItemObj> myList = new ArrayList<ItemObj>();   
req.setAttribute("list", myList);
req.getRequestDispatcher("page.jsp").forward(req,resp);

In the JSP:

<% List<ItemObj> myList = (ArrayList<ItemObj>) request.getParameter("list"); %>

However, I keep getting an error: Cannot cast from String to List. I have found sources that indicate that I can cast as such:

how to send ArrayList from jsp to servlet

Send array of objects from servlet to JSP

What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49

4 Answers4

1

Instead of request.getParameter use

<% List<ItemObj> myList = (ArrayList<ItemObj>) request.getAttribute("list"); %>

request.getParameter is used to retrieve form parameters.

Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52
1

You are setting list as request attribute and getting it back as request parameter

Use <% List<ItemObj> myList = (ArrayList<ItemObj>) request.getAttribute("list"); %>

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
1

Sotirious is spot-on. You need to call getAttribute.

But beyond that, you should be privy to the scope of your attribute: is it scoped to (i) page, (ii) request, (iii) session, or (iv) application?

I don't know the default scope off-hand, but you might want to look into that.

Kode Charlie
  • 1,297
  • 16
  • 32
0

You need request.getAttribute() instead of request.getParameter()

Sagar Sane
  • 101
  • 1
  • 5