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
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.
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
Joined: Jul 06, 2005
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...
Joined: Sep 28, 2013
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