• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot cast from String to ArrayList(JSP)

 
Omar Perez
Ranch Hand
Posts: 36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jack Numen
Ranch Hand
Posts: 42
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That book is full of errors.

That should look like:



And then it will work.
 
Stefan Evans
Bartender
Posts: 1780
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1801
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1780
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic