File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes Typecasting problem, dont understand why! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Typecasting problem, dont understand why!" Watch "Typecasting problem, dont understand why!" New topic

Typecasting problem, dont understand why!

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
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

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.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jenna Thomas

Joined: Oct 03, 2005
Posts: 27
Thanks Ben, checking out the JDT compiler for JSP now.
I agree. Here's the link:
subject: Typecasting problem, dont understand why!
It's not a secret anymore!