Spring Boot - Custom Error Page in JSP

[Last Updated: Nov 4, 2018]

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

Create a controller

public class MyController {

  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.


<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
table td{
border:solid 1px #888;
    <h1>My Error Page</h1>

Spring Boot 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.


spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp

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

public class SpringBootMain extends SpringBootServletInitializer {

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

  public static void main(String[] args) {;
mvn spring-boot-run


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


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 1.5.10.RELEASE
    Corresponding Spring Version 4.3.14.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • spring-boot-starter-tomcat : Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web.
  • 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
              • error.jsp

    See Also