File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Cannot cast from String to ArrayList(JSP) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Cannot cast from String to ArrayList(JSP)" Watch "Cannot cast from String to ArrayList(JSP)" New topic

Cannot cast from String to ArrayList(JSP)

Omar Perez
Ranch Hand

Joined: Jul 08, 2011
Posts: 36
Good day,

I'm actually trying to complete the excersise of the Servlets and JSP book in page 303 but I'm getting the following error in Eclipse Cannot cast from String to ArrayList(JSP).

Here is the code

The error as it appears in line <% ArrayList al = (ArrayList)request.getParameter("Names"); %>

I don't know what's wrong, because is exactly as is written in the solution in the book.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19844

A request parameter is never a List (or ArrayList), but always a String*. If the book says that you should cast the request parameter to an ArrayList, then that book has an error in it. Are you sure you shouldn't read an attribute instead of a parameter?

* Or a String[] when you're using getParameterValues

How To Ask Questions How To Answer Questions
Jack Numen
Ranch Hand

Joined: Nov 09, 2011
Posts: 42

Thats a valid error since request.getParameter returns a String Object (please go through the API-documentation of HttpServletRequest).
You cannot cast a string to an ArrayList.

If you want to fetch more than one value in request object try using request.getParameterNames (returns enumeration)/ request.getParameterValues()(returns String array).

I agree. Here's the link:
subject: Cannot cast from String to ArrayList(JSP)