Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
    Bookmark Topic Watch Topic
  • New Topic

foreach not working in scriptlet  RSS feed

 
Jenna Thomas
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Jenna Thomas
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Sorry for the wrong subject line. Please feel free to delete this post. Have reposted with the correct subject line. Thanks!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Jenna,
If you click on the Edit link (notepad and pencil graphic) in the top post, you can edit the subject line.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!