Search code examples
javaspringspring-bootjsp

Spring Boot hangs indefinitely when serving JSP content


The app previously worked on another machine, so the code should all be valid. I'm having difficulties porting it to another machine. Namely, the problem is that when it's time to serve a JSP view, the app hangs indefinitely.

The controller looks like this:

@RequestMapping(value = "", method = RequestMethod.GET)
    public String bookings(BookingFilterRequest bookingFilterRequest,
                           PeriodRequest periodRequest,
                           Model model,
                           BindingResult bindingResult) {
        // Fetch data from database, do things
        return "booking/bookings"; // serve JSP view
    }

The code runs fine up until the return "booking/bookings" line, which is where it hangs. The JSP view is in the correct folder, otherwise it would give a not found error.

I tried installing the same java and gradle versions that the previous server had, with no change.

Version information (default installed versions on new server):

openjdk 11.0.4 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode)
Gradle 4.4.1

Old server had java 10.0.2 and gradle 5.0.

How do I get it to run without hanging?

Update:

If I wait about 10 minutes, then it magically starts working. But restarting the application makes it hang again, until you wait 10 minutes again.

Update2:

If I put console print lines in the JSP file, then they are not printed out. Meaning, the JSP does not get executed.


Solution

  • Solution:

    Looks like it was a bug in Tomcat. I was finally able to get it working using Jetty. Steps:

    1) Use Jetty instead of Tomcat. In build.gradle dependencies section, comment out org.springframework.boot:spring-boot-starter-web and org.apache.tomcat.embed:tomcat-embed-jasper lines and add:

    compile("org.springframework.boot:spring-boot-starter-web") {
       exclude module: "spring-boot-starter-tomcat"
    }
    compile("org.springframework.boot:spring-boot-starter-jetty")
    compile("org.eclipse.jetty:apache-jsp")
    compile "javax.servlet:jstl:1.2"
    

    2) Build the war file. Jar files didn't work for me, since Jetty didn't find the JSP files when I used jar. If you cant build war files, then make sure you have this in your build.gradle:

    plugins {
      id 'war'
    }
    

    It needs to be right after the buildscript code block.

    Build the war file using

    gradle build war

    It will appear in your build/libs/ directory.

    3) Run the application:

    java -jar application.war


    Extra stuff

    Here's my complete build.gradle file if it's useful to anyone. I'm aware there are a few unnecessary lines there.

    
    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE")
        }
    }
    
    plugins {
       id 'war'
    }
    apply plugin: 'java'
    apply plugin: 'application'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    mainClassName = 'com.project.foo.bar.Application'
    
    
    bootJar {
        baseName = 'bar'
        version =  '0.1.0'
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile("org.springframework.boot:spring-boot-starter-web") {
          exclude module: "spring-boot-starter-tomcat"
        }
        compile("org.springframework.boot:spring-boot-starter-jetty")    
        //compile("org.springframework.boot:spring-boot-starter-web")
        //compile("org.springframework.boot:spring-boot-starter-security")
    
        // persistence
        compile("org.springframework:spring-orm:4.0.3.RELEASE")
        compile("commons-dbcp:commons-dbcp:1.4")
        compile("org.hibernate:hibernate-core:4.3.11.Final")
        compile("org.hibernate:hibernate-entitymanager:4.3.11.Final")
        runtime('mysql:mysql-connector-java')
    
        // servlets
        compile("javax.servlet:javax.servlet-api:4.0.1")
        compile("javax.servlet.jsp:javax.servlet.jsp-api:2.3.1")
        // runtime("org.apache.tomcat.embed:tomcat-embed-jasper")
        compile("org.eclipse.jetty:apache-jsp")
        compile "javax.servlet:jstl:1.2"
    }