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

Improved ForEach loop in Scriptlet/JSP

Robert Pruett

Joined: May 25, 2006
Posts: 3
Hey folks, another question - hopefully someone can shed some light on this for me. I'm attempting to implement an improved foreach loop using scriptlets in JSP and I've run into something of an issue.

I'm rendering a tree of folders in my view using a LinkedList of CabinetTreeObj objects. Each of the CabinetTreeObj objects in the LinkedList contains a CabinetFolder object, which contains information such as its parent, name, description, etc, and an Integer grouping, used to identify its depth.

This is instantiated and populated within my servlet, which then simply passes the LinkedList to the view as an attribute, and is rendered accordingly as it iterates through.

I originally used <c:ForEach> to iterate through the list and it worked fine... but I had a few problems communicating values between JSTL and scriptlet code using PageContext. Instead, I decided to write the whole section as a scriptlet.

When using a scriptlet, instead of iterating through the list as it did using <c:forEach>, it threw an exception.

I'm using Tomcat 5.5. I used System.getProperty("java.vm.version") to confirm the compiler version as 1.5.0_06-b05 (verifying that the improved for loop is supported in JSP, hopefully!).

Judging from the exception, it seems to expect:
for (iterator; condition; increment)

Rather than:
for (Object name: names)

The exception thrown is as follows:

May 30, 2006 2:59:42 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 110 in the jsp file: /WEB-INF/web/filecabinet/cabinet.jsp
Generated servlet error:
Syntax error on token ":", ; expected

An error occurred at line: 110 in the jsp file: /WEB-INF/web/filecabinet/cabinet.jsp
Generated servlet error:
Syntax error on token ")", delete this token

An error occurred at line: 110 in the jsp file: /WEB-INF/web/filecabinet/cabinet.jsp
Generated servlet error:
Syntax error, insert "; ) Statement" to complete ForStatement

My scriptlet code is as follows:

Thanks in advance!
[ May 30, 2006: Message edited by: Robert Pruett ]

Robert Pruett
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

The compiler that the container is using to translate the JSPs is probably a JDK 1.4 compiler.

From the error messages I see that you are using Tomcat, so I've moved this to the Tomcat forum for instructions on setting things up correctly.

I'm not sure of the setup myself since I no longer include scriptlets in JSP pages and haven't tripped over this issue.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Improved ForEach loop in Scriptlet/JSP
It's not a secret anymore!