My application was first downloading the jsp file instead of rendering it on page. I was getting a 200 status code in console but 404 on the page. I looked around and found I was suppose to add the dependencies for tomcat-jasper and tomcat-embed but now am getting a class cast exception saying that jspservlet can’t be cast to javax servlet. Any help appreciated. Thanks!
Getting an ApplicationContextException
:
Caused by: javax.servlet.ServletException: Class [org.apache.jasper.servlet.JspServlet] is not a Servlet
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1054) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
at org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedContext.load(TomcatEmbeddedContext.java:82) ~[spring-boot-2.5.2.jar:2.5.2]
... 32 common frames omitted
Caused by: java.lang.ClassCastException: org.apache.jasper.servlet.JspServlet cannot be cast to javax.servlet.Servlet
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1049) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
... 34 common frames omitted
This happens when I have this in my pom:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.1.0-M2</version>
<scope>runtime</scope>
</dependency>
When this is taken out of my pom everything works fine, hits the controller and returns the ModelAndView but downloads the jsp instead of actually displaying the page.
CodePudding user response:
TL;DR: Use
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
There are a couple of problems with the dependency you added:
- You should use
tomcat-embed-jasper
instead oftomcat-jasper
: it depends ontomcat-embed-core
(which is already used by Spring Boot) instead oftomcat-catalina
(which is not used by Spring Boot). Including the latter causes problems like in this question. - Jasper 10.1 implements JSP 3.0, which is not compatible with Servlet 4.0 provided by Tomcat 9.0 (cf. this question). Since
spring-boot-parent
manages these dependencies, you should omit the<version>
tag and use the version chosen by Spring Boot (9.0.48 for the version you are using, but it will keep in sync, when you update Spring Boot), - If you deploy your application as WAR file in an external servlet container, you want to use the version of JSP engine provided by the servlet container, instead of your own. Therefore you should set the scope to
provided
.