File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes foreach not working in scriptlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Reply locked New topic

foreach not working in scriptlet

Jenna Thomas

Joined: Oct 03, 2005
Posts: 27
Newbie to jsp/tomcat/servlets..

I was able to compile the following in my tomcat5.5 installation(java version in use is jdk1.5.07 - i checked)-
In my servlet
List instList=instbeerExpert.getBrands(c);
for(Object beerbrand:instList){
out.println("<br>Try "+beerbrand);

In the jsp
List styles=request.getAttribute("styles");
for(Object style:styles){
out.print("<br>Try: "+style);
The above jsp gives the following error-
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 8 in the jsp file: /result.jsp
Generated servlet error:
Type mismatch: cannot convert from Object to List

The error goes away when i typecast-
List styles=(List)request.getAttribute("styles");

Why do i have to do this typecasting?

And oh, yes, the code is from the HF Servlets and JSP book, thats why it probably looks familiar :P

Jenn SCJP1.5
Jenna Thomas

Joined: Oct 03, 2005
Posts: 27
Sorry for the wrong subject line. Please feel free to delete this post. Have reposted with the correct subject line. Thanks!
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

If you click on the Edit link (notepad and pencil graphic) in the top post, you can edit the subject line.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: foreach not working in scriptlet
It's not a secret anymore!