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.


  1. nice article. Thank you. Can you explain elaborately plz

  2. I suspecting only I have to use in web.xml


    but it does not receiving this code . a cross sign is appears in the web.xml file in the project window.I am using netbeans 7.3.1

    I also changed/replaced


    from your github src folder which was just like


    what can i do now. plz help me

    1. I can not realizing why all tag sign are vanished

  3. Thank you.

    I think:

    MessageFormat.format("{0} returned for {1} with message {3}"

    Needs to be:

    MessageFormat.format("{0} returned for {1} with message {2}"

    As '3' does not exist.

    And, do you know of a way that 'error-page' can be set programatically and not in the web.xml file?


Post a Comment

Popular posts from this blog

Different ways of validating @RequestBody in Spring MVC with @Valid annotation