• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Typecasting problem, dont understand why!

 
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat 5.5 ships with it's own compiler (JDT from the Eclipse project) for compiling JSPs.
This compiler doesn't fully understand all of the 1.5 sytax features yet.

Either move that scriptlet logic into your servlets and beans (which will work fine if you compile them with a 1.5 compiler), use the 1.4 syntax for your for-loops (and cast by hand), or (I'm not exactly sure how you do this) configure Tomcat to use Sun's javac program. It should then undersand the new for loop syntax.
 
Jenna Thomas
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben, checking out the JDT compiler for JSP now.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!