About Rafal Borowiec

Rafal is an IT specialist with about 8 years of commercial experience, specializing in software testing and quality assurance, software development, project management and team leadership.

HOW-TO: Custom error pages in Tomcat with Spring MVC

Default Tomcat error pages look scary. In addition, they may expose valuable information including server version and exception stack trace. Servlet specification provides a way to configure an exceptional behavior through web.xml. One can configure either reaction on a specific Java exception or to a selected Http response code(s).

error-page element specifies a mapping between an error code or exception type to the path of a resource in the Web application:

 <!-- Prior to Servlet 3.0 define either an error-code or an exception-type but not both -->
  <!-- Define error page to react on Java exception -->
  <!-- The location of the resource to display in response to the error will point to the Spring MVC handler method -->
 <!-- With Servlet 3.0 and above general error page is possible -->

Having the custom error pages defined in our web.xml, we need to add the Spring MVC @Controller. The customError handler method wraps the information, that we retrieve from the request, and returns it to the view.

class CustomErrorController {

 public String customError(HttpServletRequest request, HttpServletResponse response, Model model) {
  // retrieve some useful information from the request
  Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
  Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
  // String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");
  String exceptionMessage = getExceptionMessage(throwable, statusCode);

  String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
  if (requestUri == null) {
   requestUri = "Unknown";

  String message = MessageFormat.format("{0} returned for {1} with message {3}", 
   statusCode, requestUri, exceptionMessage

  model.addAttribute("errorMessage", message);  
  return "customError";

 private String getExceptionMessage(Throwable throwable, Integer statusCode) {
  if (throwable != null) {
   return Throwables.getRootCause(throwable).getMessage();
  HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
  return httpStatus.getReasonPhrase();

The produced message may look like following: 404 returned for /sandbox/bad with message Not Found.

To see the code in action, browse the source code of  Spring MVC Quickstart Archretype, or even better, generare a new project with it.

Reference: HOW-TO: Custom error pages in Tomcat with Spring MVC from our JCG partner Rafal Borowiec at the Codeleak.pl blog.
Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

Leave a Reply

+ four = 8

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: