10

I am using tomcat and I would like, whenever I go to a direction that is not handled by a servlet, do other stuff insead of showing the default error:

type Status report

message /test

description The requested resource is not available.

Where can I handle this¿?

Thank you in advance

slm
  • 15,396
  • 12
  • 109
  • 124
aDoN
  • 1,877
  • 4
  • 39
  • 55
  • possible duplicate of [Custom Error Page in Tomcat 7 for Error Code 500](http://stackoverflow.com/questions/15987212/custom-error-page-in-tomcat-7-for-error-code-500) – jas_raj Jan 09 '15 at 11:37
  • @jas_raj, not a duplicate, as people here are are wanting a solution for the docroot folder where the app already has a custom 404/ but the app root is below docroot. – mckenzm Oct 25 '22 at 20:51

3 Answers3

17

Define an error page in web.xml:

<error-page>
 <error-code>404</error-code>
 <location>/path/to/your/page.html</location>
</error-page>

Update

  1. No does not need anyting spec. Can be anything, html, jsp, jsf ...
  2. No
  3. Can be placed anywhere, often at the bottom
  4. Between brackets

You can define error pages by their http status (404, 500,...) or by defining the fully qualified exception name (java.lang.Exception, java.io.FileNotFoundException ...). If you are using Servlet 3.x you can even omit the error-code/error-classname part to define a default error page.

Stefan
  • 12,108
  • 5
  • 47
  • 66
  • Does do error page need to be somehow specific¿? Any header or something special¿? And other question, where in the web.xml should it be placed¿? Between what brackets¿? Or just at the end¿? Thanks – aDoN Jan 09 '15 at 12:10
  • 1
    You can place it in anywhere inside in the web.xml. The header is automatically applied. The path is relative to WebRoot. – Paul McClean Dec 07 '17 at 09:50
3

Here is a minimal web.xml which you can put in a webapps folder (if you don't want to change 404 pages globally). This will allow you to e.g. redirect all requests to a new folder.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <error-page>
    <error-code>404</error-code>
    <location>/redirect.jsp</location>
  </error-page>
</web-app>

Note that web.xml must be put in .../webapps/YourFolder/WEB-INF/web.xml.

In the redirect.jsp. You would put something like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
    <title>Moved</title>
</head>
<%
    // get the requested URI
    String requestedLocation = request.getRequestURI();
    // rewrite to new location
    String newLocation = requestedLocation.replaceAll("^/Old", "/New");

    // 301 - permanent redirect
    response.setStatus(response.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", newLocation);
%>
<body>
&rarr; <a href="<%=newLocation%>"><%=newLocation%></a>
</body>
</html>
Nux
  • 9,276
  • 5
  • 59
  • 72
  • But where does the redirect.jsp go? – coder_3476 Dec 03 '20 at 20:44
  • The problem with this is it needs to go in a webapps folder. What can I do about docroot if I have a custom 404 for example.com/myapp/crap but not for example.com/crap ? – mckenzm Oct 25 '22 at 20:53
2

Since Tomcat 9 there is different error handling configuration.

You have to:

  • create simple html error page
  • save it in some directory(for example webapps/ROOT/error_page.html)
  • add valve configuration to server.xml in Host part.
<Valve className="org.apache.catalina.valves.ErrorReportValve" errorCode.404="webapps/ROOT/error_page.html" errorCode.0="webapps/ROOT/error_page.html" showReport="false" showServerInfo="false" />

Please refer: https://stackoverflow.com/a/55702749/2532710 or Tomcat documentation: https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Error_Report_Valve