Jim C
Jim C

Reputation: 4385

JasperException: Unable to compile class for JSP - Tomcat 8 plus Spring MVC

I followed successfully the excellent tutorial from Oracle showing how to create an embedded tomcat application which I could start via java -jar \targeg\"myExecutableJar". In such tutorial there isn't Spring at all. (Oracle Tutorial

Then I edited the index.jsp as showed bellow. Obviously, I added in pom.xml all Spring lib necessary.

After this, I am getting the error from my question subject. I have read several question with similar point none of them fixed my issue. FOr instance, in certain question someone told to check ecj so I added it in pom. Other answer recommend to use the most recent Tomcat available. I did it.

It seems to me that there might exist some conflict probably between some embedded tomcat lib and other one but I can't imagine which one.

Other possibility might be related to some issue with the spring context especially because it is an embedded tomcat but I can't guess anything else to try.

Am I missing any extra configuration? Kindly, note in the image below that I leave the context.xml under META-INF exactly as automatically generated and I place all the beans definition in App-servlet.xml.

Project Structure and the main method

index.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>

<body>



<h2>Uploading a file.. </h2>

      <form:form method="POST" action="uploadFile" enctype="multipart/form-data">

              Upload your file please:

              <input type="file" name="file" />

              <input type="text" name="name">

              <input type="submit" value="Upload" />

              <form:errors path="file" cssStyle="color: #ff0000;" />

       </form:form>



</body>

</html>

App-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="

   http://www.springframework.org/schema/beans    

   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context-4.2.xsd">



       <context:component-scan base-package="com.example.employees" />



       <bean

              class="org.springframework.web.servlet.view.InternalResourceViewResolver">

              <property name="prefix" value="/WEB-INF/" />

              <property name="suffix" value=".jsp" />

       </bean>



       <bean id="multipartResolver"

              class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />



</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">



       <display-name>Spring MVC Application</display-name>









       <servlet>

              <servlet-name>App</servlet-name>

              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

              <init-param>

                     <param-name>contextConfigLocation</param-name>

                     <param-value>/WEB-INF/App-servlet.xml</param-value>

              </init-param>

              <load-on-startup>1</load-on-startup>

       </servlet>



       <servlet-mapping>

              <servlet-name>App</servlet-name>

              <url-pattern>/</url-pattern>

       </servlet-mapping>

       <welcome-file-list>

              <welcome-file>index.jsp</welcome-file>

       </welcome-file-list>

</web-app>

pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

       <modelVersion>4.0.0</modelVersion>

       <groupId>com.example.employees</groupId>

       <artifactId>employees-app</artifactId>

       <packaging>war</packaging>

       <version>1.0-SNAPSHOT</version>

       <name>employees-app Maven Webapp</name>

       <url>http://maven.apache.org</url>

       <properties>

              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

              <tomcat.version>8.0.32</tomcat.version>

              <spring.version>4.2.5.RELEASE</spring.version>

       </properties>

       <dependencies>



              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-web</artifactId>

                     <version>${spring.version}</version>

              </dependency>



              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-webmvc</artifactId>

                     <version>${spring.version}</version>

              </dependency>



              <dependency>

                     <groupId>commons-fileupload</groupId>

                     <artifactId>commons-fileupload</artifactId>

                     <version>1.3.1</version>

              </dependency>



              <dependency>

                     <groupId>org.apache.tomcat.embed</groupId>

                     <artifactId>tomcat-embed-core</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat.embed</groupId>

                     <artifactId>tomcat-embed-logging-juli</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat.embed</groupId>

                     <artifactId>tomcat-embed-jasper</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat</groupId>

                     <artifactId>tomcat-jasper</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat</groupId>

                     <artifactId>tomcat-jasper-el</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat</groupId>

                     <artifactId>tomcat-jsp-api</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>jstl</groupId>

                     <artifactId>jstl</artifactId>

                     <version>1.2</version>

              </dependency>

              <dependency>

                     <groupId>javax.servlet</groupId>

                     <artifactId>javax.servlet-api</artifactId>

                     <version>3.0.1</version>

              </dependency>

              <dependency>

                     <groupId>org.eclipse.jdt.core.compiler</groupId>

                     <artifactId>ecj</artifactId>

                     <version>4.5.1</version>

              </dependency>



       </dependencies>

       <build>

              <finalName>employees-app</finalName>

              <resources>

                     <resource>

                           <directory>src/main/webapp</directory>

                           <targetPath>META-INF/resources</targetPath>

                     </resource>

              </resources>

              <plugins>

                     <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-compiler-plugin</artifactId>

                           <version>3.5.1</version>

                           <inherited>true</inherited>

                           <configuration>

                                  <source>1.8</source>

                                  <target>1.8</target>

                           </configuration>



                     </plugin>

                     <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-assembly-plugin</artifactId>

                           <configuration>

                                  <descriptorRefs>

                                         <descriptorRef>jar-with-dependencies</descriptorRef>

                                  </descriptorRefs>

                                  <finalName>employees-app-${project.version}</finalName>

                                  <archive>

                                         <manifest>

                                                <mainClass>com.example.employees.Main</mainClass>

                                         </manifest>

                                  </archive>

                           </configuration>

                           <executions>

                                  <execution>

                                         <phase>package</phase>

                                         <goals>

                                                <goal>single</goal>

                                         </goals>

                                  </execution>

                           </executions>

                     </plugin>

              </plugins>

       </build>

</project>

log:

type Exception report

message Unable to compile class for JSP

description The server encountered an internal error that prevented it from fulfilling this request.

exception

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

        org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:600)

        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)

        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NullPointerException

        org.apache.jasper.JspCompilationContext.getTldResourcePath(JspCompilationContext.java:551)

        org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:420)

        org.apache.jasper.compiler.Parser.parseDirective(Parser.java:479)

        org.apache.jasper.compiler.Parser.parseElements(Parser.java:1435)

        org.apache.jasper.compiler.Parser.parse(Parser.java:139)

        org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)

        org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)

        org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)

        org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)

        org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)

        org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)

        org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)

        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)

        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

************ Added

enter image description here

Upvotes: 1

Views: 23440

Answers (2)

Lucky
Lucky

Reputation: 17345

This might be due to the servlet.jar jar conflict. Tomcat already comes in with the servlet api library. Try to add the <scope>provided</provide> for the servlet dependency in your pom.xml.

<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
     <version>3.0.1</version>
     <scope>provided</scope>
</dependency>

This dependency is already provided by your container(ie. Tomcat) so you don't have to include this in your deployed application again.

Other things you can try:

  • Try Project -> Clean and re-run the project
  • You should require at least JSP 2.1 and mark it as provided in your POM

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    
  • One possible cause is if a temp file cannot be written to the current directory.

  • And this article specific for 1.8 version of JDK.
  • In Tomcat 8, Jasper initialization is implemented as a standard ServletContainderInitializer (check Servlet specification). So you should ensure that org.apache.jasper.servlet.JasperInitializer.onStartup is invoked during web app startup. Source

  • Setting the context with, context.addServletContainerInitializer(new JasperInitializer(), null);
  • Related others https://github.com/spring-projects/spring-boot/issues/962 & https://github.com/bmuschko/gradle-tomcat-plugin/issues/132

Upvotes: 5

Akash Panday
Akash Panday

Reputation: 59

remove following dependency from pom.xml . add manually project run time .

          <dependency>

                 <groupId>org.apache.tomcat.embed</groupId>

                 <artifactId>tomcat-embed-core</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat.embed</groupId>

                 <artifactId>tomcat-embed-logging-juli</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat.embed</groupId>

                 <artifactId>tomcat-embed-jasper</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat</groupId>

                 <artifactId>tomcat-jasper</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat</groupId>

                 <artifactId>tomcat-jasper-el</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat</groupId>

                 <artifactId>tomcat-jsp-api</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

Upvotes: 1

Related Questions