• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Improved ForEach loop in Scriptlet/JSP

 
Robert Pruett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic