Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat is not redirecting to 400 bad request custom error page

We have tomcat 9 web server in production. The problem we are facing is we want to show the custom error page for our application if tomcat receives any malformed URL as follows

http://URL/|

or

http://URL/[

I have added error page tag in web.xml of tomcat application server as follows

<error-page>
   <error-code>400</error-code>
   <location>/error.html</location>
 </error-page>
 <error-page>
   <error-code>404</error-code>
   <location>/error.html</location>
 </error-page>

and we have error.html in our application ROOT folder in webapps directory of tomcat.

Whenever the user tries to request any nonexistent webpage he receives error page correctly corresponding to 404 error code as specified above.

Real Problem is tomcat not able to redirect to error.html when the user enters malformed URL in the browser like http://URL/|

Tomcat is showing the default 400 bad request error page instead of the custom error page.

like image 429
Pawan Patil Avatar asked Oct 15 '18 10:10

Pawan Patil


People also ask

How do I create a custom error page for when Tomcat is down for maintenance?

On the ErrorDocument line, we specify the error code, which in this case is a 503. After that, we can specify either a text message or the page to display. I'll specify to display the "maintenance-message. html" page, which I create in the Apache document root directory, which in my case is in my Apache2.

Where is the default error page in Tomcat?

The default Tomcat error page exposes the Tomcat version that SDM is using. After the above changes, when the SDM URL is accessed via http://<SD server hostname>:<port#>/abc, a screen similar to the following will appear that does not have the Tomcat version listed.

What is ErrorReportValve?

Class ErrorReportValveImplementation of a Valve that outputs HTML error pages. This Valve should be attached at the Host level, although it will work if attached to a Context. HTML code from the Cocoon 2 project.


1 Answers

This is not a bug, it is a configuration issue.

Tomcat 9 has improved the error handling so that more errors, particularly those those occur before request processing starts, are passed to the standard error handling mechanism rather than just returning an error code.

In the case of malformed URIs, it is not possible for Tomcat to parse the URI hence the URI cannot be mapped to a web application. Tomcat is, therefore, unable to trigger the application level error handling because it cannot identify an application. In this case, the error is handled by the ErrorReportValve which can be configured in server.xml.

A recent(ish) addition to the ErrorReportValve is the ability to define static web pages (i.e. no Servlets or JSPs, just HTML) per status code and/or Exception type in a similar manner to the per web application error page configuration.

Example Error Report Valve:

<Valve className="org.apache.catalina.valves.ErrorReportValve"
        errorCode.400="webapps/ROOT/error400.html"
        errorCode.0="webapps/ROOT/errorOthers.html"
        showReport="false"
        showServerInfo="false" />
like image 118
Mark Thomas Avatar answered Sep 17 '22 08:09

Mark Thomas