Home > Back-end >  spring jsp app works in tomcat but respond 404 in embedded tomcat
spring jsp app works in tomcat but respond 404 in embedded tomcat

Time:04-28

I have a spring-mvc jsp project that perfectly works on external tomcat server. I'm trying to make a runable war file with using spring-boot and embedded tomcat server. But when I try to call GET /student I recieve 404 error and no error message in console.

project screen

@Bean
public UrlBasedViewResolver setupViewResolver() {
   UrlBasedViewResolver resolver = new UrlBasedViewResolver();
   resolver.setPrefix("/WEB-INF/views/");
   resolver.setSuffix(".jsp");
   resolver.setViewClass(JstlView.class);
   return resolver;
}

build.gradle

plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'war'
}

description = 'Teaching Academy Web Application.'

ext.tomcatHome = 'D:/Soft/apache-tomcat-9.0.62'
ext.tomcatWebapps = "$tomcatHome/webapps"


war {
archiveName = 'academy-web.war' 
}

bootWar {
    archiveName = 'academy-web-bootable.war'
}

task deployToTomcat(type: Copy, dependsOn: 'build'){    
    from war
    into "$tomcatWebapps"   
}

dependencies {
//  implementation 'org.springframework:spring-webmvc:4.0.3.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
    implementation 'javax.servlet:jstl'


    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

    implementation 'log4j:log4j:1.2.17' 
    implementation "javax.servlet:javax.servlet-api:4.0.1"
}

CodePudding user response:

This is the expected behavior according to the development team of Spring-boot. The problem here is not the tomcat server if embedded or not, but the type of application that you deploy.

In external tomcat you package and deploy your application as war file, which spring-boot team has said that is able to support jsp pages.

In embedded tomcat probably you mean that you deploy your application as a standalone .jar file with an embedded server and here spring-boot team has mentioned that this is one of the limitations where jsp technology will not work. Relevant Spring Boot documetation

Also checking closer your question you mention

I'm trying to make a runable war file with using spring-boot and embedded tomcat server.

The embedded server will not work as expected with a war file. The war will contain servlet only information about the application, not the embedded server and not a standalone application that could start a server. So to sum up the war file when executed will not start a server and therefore the 404 response you get when you visit the application.

  • Related