I have a Spring Boot web application up and running using embedded Tomcat (the default). When it serves up JSP files as part of rendering the view I specified in my controller, the JSPs are not being rendered as such, and instead print out the contents. For example:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html lang="en"> <head></head> <body>Test</body> </html>
When the view is rendered in the browsers, the contents above are displayed, instead of the expected contents:
Test
As per convention, we place our JSP files in the ${project. basedir}/main/webapp/WEB-INF/jsp/ directory.
Project Dependencies We want to use JSP as the view. Default embedded servlet container for Spring Boot Starter Web is tomcat. To enable support for JSP's, we would need to add a dependency on tomcat-embed-jasper.
In order to deploy Java Server Pages (JSP) files, you must place them in the root (or in a subdirectory below the root) of a Web application.
Because JSPs are part of the J2EE standard, you can deploy JSPs on a variety of platforms, including WebLogic Server.
Make sure that your pom.xml
specifies the Tomcat JSP dependency as follows:
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
It seems that embedded Tomcat treats the JSP rendering as optional.
As mentioned below, this JAR is sometimes necessary as well:
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>provided</scope> </dependency>
(I added provided since this JAR should be included by the servlet container.
You will need not one but two dependencies (jasper and jstl) in your pom.xml
for this to work.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With