This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Reply locked New topic
Author

foreach not working in scriptlet

Jenna Thomas
Greenhorn

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


Cheers!
Jenn SCJP1.5
Jenna Thomas
Greenhorn

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
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Jenna,
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
 
Consider Paul's rocket mass heater.
 
subject: foreach not working in scriptlet
 
Similar Threads
Object typecasting
help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp
CHAPTER 3 HF SERVLETS & JSP --- result.jsp compilation issues
HTTP Status 500 - error running servlet with JSP
Typecasting problem, dont understand why!