Close

Spring Boot - Custom Error Page in JSP

[Updated: Feb 4, 2018, Created: Feb 3, 2018]

This example shows how to replace the default whitelabel error page with a custom JSP error page in Spring Boot.

Create a controller

@Controller
public class MyController {

  @RequestMapping("/")
  public void handleRequest() {
      throw new RuntimeException("test exception");
  }
}

A custom JSP error page

Spring Boot by default provides /error mapping where all exception/errors are forwarded, that means we can map errors to a JSP page with the name error.jsp.

Following attributes are available for our view to display:
timestamp, status, error, exception, message, trace, path.

src/main/webapp/WEB-INF/views/error.jsp

<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
    <h1>My Error Page</h1>
    <table>
        <tr>
            <td>Date</td>
            <td>${timestamp}</td>
        </tr>
        <tr>
            <td>Error</td>
            <td>${error}</td>
        </tr>
        <tr>
            <td>Status</td>
            <td>${status}</td>
        </tr>
        <tr>
            <td>Message</td>
            <td>${message}</td>
        </tr>
        <tr>
            <td>Exception</td>
            <td>${exception}</td>
        </tr>
        <tr>
            <td>Trace</td>
            <td>
                <pre>${trace}</pre>
            </td>
        </tr>
    </table>
</body>
</html>

Spring Boot application.properties file

We have to disable the default whitelabel page for our jsp error page to work. We are additionally enabling the stacktrace to be included as expression attribute to our view.

src/main/resources/application.properties

spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp
server.error.whitelabel.enabled=false
server.error.include-stacktrace=always

The default value of 'server.error.include-stacktrace' is 'never'. Setting this attribute to 'always' for default whitelabel page does not work because that view does not include ${trace} expression (if interested see the source code of nested class WhitelabelErrorViewConfiguration of ErrorMvcAutoConfiguration).

Main class

@SpringBootApplication
public class SpringBootMain extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
      return builder.sources(SpringBootMain.class);
  }

  public static void main(String[] args) {
      SpringApplication.run(SpringBootMain.class);
  }
}
mvn spring-boot-run

Output

To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

Accessing 'http:/localhost:8080/'

Accessing unmapped URL, say 'http:/localhost:8080/other'

Deploying to Tomcat server:

We are going to deploy our example war via 'tomcat7-maven-plugin' (included in the pom.xml).

Run the tomcat server instance('<tomcat-home>/bin/startup.bat') and deploy the war (check out how to deploy war using tomcat maven plugin tutorial):

mvn clean tomcat7:deploy

Output:

Accessing the example app at http://localhost:8080/custom-jsp-error-page/

Running Boot executable war:

Our JSP error page also works if we run our example application from executable war file. To package war:

projectRoot> mvn package

To run war:

projectRoot\target> java -jar custom-jsp-error-page-1.0-SNAPSHOT.war

Accessing 'http://localhost:8080/' will return the same error page

Example Project

Dependencies and Technologies Used:

  • spring-boot-starter-web 1.5.10.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container [Description from spring-boot-starter-web-1.5.10.RELEASE.pom]
    Corresponding Spring version: 4.3.14.RELEASE
  • spring-boot-starter-tomcat 1.5.10.RELEASE: Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web [Description from spring-boot-starter-tomcat-1.5.10.RELEASE.pom]
    Corresponding Spring version: 4.3.14.RELEASE
  • tomcat-embed-jasper 8.5.27: Core Tomcat implementation.
  • jstl 1.2 javax.servlet:jstl
  • JDK 1.8
  • Maven 3.3.9

Boot Custom Jsp Error Page Example Select All Download
  • custom-jsp-error-page
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
        • webapp
          • WEB-INF
            • views

See Also