This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Typecasting problem, dont understand why!

 
Jenna Thomas
Greenhorn
Posts: 27
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 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
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben, checking out the JDT compiler for JSP now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic