0

I am trying to make the SpringBoot tutorial from Youtube. But in IntelliJ + Maven, not Eclipse. At the 1 hour mark, they add the jasper dependency and then run the program and it works nicely.

Before I add the jasper dependency, I have no errors. After I add it, I get this error:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.NoClassDefFoundError: jakarta/servlet/jsp/JspFactory. 

This is my main App File:


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class CoursesWebApp {

    public static void main(String[] args) {
        SpringApplication.run(CoursesWebApp.class, args);
    }

}

and the controller file

package com.example.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CoursesController {

    @RequestMapping("/courses")
    public void coursesDisplay() {
        System.out.println("Welcome to courses!");
        //return "course.jsp";
    }
}

This is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot</name>
    <description>Project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>10.0.8</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

I have installed Tomcat version 10.0.8 and matched the Jasper with the same version.

I tried to make public class CoursesWebApp extends SpringBootServletInitializer, didn't work.

There is the annotation for @SpringBootApplication.

I tried to add

<dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-jasper</artifactId>
       <scope>provided</scope>
   </dependency>

With scope changed to default or compile, didn't work.

I am utterly new at this and I have no idea how to fix it. It feels like it's something small, but I can't see it.

Thanks in advance for your help!

Nexx
  • 51
  • 1
  • 2
  • 7

3 Answers3

5

Tomcat 10 implements the Servlet 5.0 and JSP 3.0 APIs which Spring Boot does not yet support. You should use Tomcat 9.0.x, which implements Servlet 4.0 and JSP 2.3, instead.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 1
    Thanks! I changed it to Tomcat 9.0.50 and it still didn't work with the above shown dependencies because I got `FileNotFoundException thrown from StandardJarScanner`. **But** I changed the `tomcat-jasper` with `tomcat-embed-jasper`, which I mentioned at the end and it worked. – Nexx Jul 21 '21 at 06:52
1

Spring boot doesn't provide inbuilt view support , we have to explicitly create views in Spring Boot project. Jasper is is used to compile JSP pages in to servlets on application servers, yet Spring boot supports Servlets 4.0 API which is not supported in Tomcat 10 and Jasper 10. Using Lower version of tomcat and Jasper solves the above issue in Boot application. Make sure you use the similar version of Tomcat and Jasper, otherwise it will again give error.

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.55</version>
</dependency>

Tomcat and jasper version must match

Ratnesh K
  • 19
  • 6
-1

Have you tried to find an answer on the net - the best way is to find by stacktrace. Check for example this answer stackoverflow