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


JavaRanch » Java Forums » Java » JSP
Bookmark "Cannot cast from String to ArrayList(JSP)" Watch "Cannot cast from String to ArrayList(JSP)" New topic
Author

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
Sheriff

Joined: Oct 27, 2005
Posts: 20077
    
  30

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
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).

Nenad Bulatovic
Greenhorn

Joined: Sep 28, 2013
Posts: 24
That book is full of errors.

That should look like:



And then it will work.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1517
    
    6
If the book has errors like that, I would suggest throwing away the book and finding another.
In fact any book that is showing you to program scriptlets into JSP should be thrown away as massively out of date.
And it doesn't even USE the scriptlet tags properly in my opinion.

In the interests of sanity:


I tested it with: [MyServer]/names.jsp?hobby=programming&names=Matthew&names=Mark&names=Luke&names=John

Changes made from original example:
- Used JSTL rather than scriptlet code.
- named parameters with lower case letters as per convention
- Used a list instead of separating names with <br> tags.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1424
    
  14

Rob Spoor wrote:Are you sure you shouldn't read an attribute instead of a parameter?

This is exactly the error the OP made. The book says request.getAttribute("names").

@Stefan, while HFSJ is showing it's age, it's still one of the best books for learning this material. In fact, in the very next chapter the book says something to the effect of "now that you know how to write scriptlets, don't ever use them". The whole point of teaching them is so the reader can support the cursed things, like I still do every day.

I would really like to see an updated version of HFSJ that drops all content about scriptlets and adds material about Java 8 (or at least 7). But Bert has been mum about whether a new edition is in the works.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1517
    
    6
Oh its HFSJ? The OP just referred to "Servlets and JSP Book", and then there was a comment to the effect of "That book is full of errors."
I didn't think it could be HFSJ at that point.

I presumed the code example was copy/pasted. Bad assumption on my part.
Despite the getParameter vs getAttribute, the fact that it has scriptlet tags on every line rather than scriptlet tags around a code block looked massively wrong.

Maybe I just have too low an opinion of some books out there...

Nenad Bulatovic
Greenhorn

Joined: Sep 28, 2013
Posts: 24
Yes, OP is talking about O'Reilly, Head First Servlets & JSP.
Amount of errata is enormous, and there's even a lot of it NOT covered in errata.
While book is brilliant in it's concept and explaining some stuff, it's almost unbelievable how erroneous is on other parts.
Just check errata confirmed and not confirmed
http://www.oreilly.com/catalog/errata.csp?isbn=9780596516680
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Cannot cast from String to ArrayList(JSP)
 
It's not a secret anymore!