When it comes for a JSP page to be compiled, Tomcat defaults to Java 1.3. Thus, if you are using any Java 5 feaures in your JSP pages, you will get an exception like this:

org.apache.jasper.JasperException: Unable to compile class for JSP

A common case is trying to use for-each loop, which is a Java 5 feature. This would generate an error message like this:

for-each loops are not supported in -source 1.3
(use -source 5 or higher to enable for-each loops)


To fix the problem you have to tell Tomcat that you want to use Java 5 as the source level for JSP compilation. Go to Tomcat's configuration directory, and edit the web.xml file. Find the "The JSP page compiler and execution servlet" section, it should look like this:

<servlet>
<servlet-name>jsp</servlet>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>


Edit it so that it looks like this one (insert the text in bold letters):

<servlet>
<servlet-name>jsp</servlet>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>compilerSourceVM</param-name>
<param-value>1.5</param-value>
</init-param>
<init-param>
<param-name>compilerTargetVM</param-name>
<param-value>1.5</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>


Save the web.xml file and restart Tomcat. It should be fixed by now.

Leave a Reply