Create Tomcat custom error pages, but not in ROOT

322 Views Asked by At

Can I put my generic error folder not in the /ROOT folder, but somewhere else?

I'm running a local Tomcat server (9.0.37) with multiple applications, similar to the folder structure below

tomcat/
      conf/
         web.xml
         server.xml
      webapps/
         ROOT/
           WEB-INF/
         app1/
           META-INF/
           WEB-INF/
              web.xml
         app2/
           META-INF/
           WEB-INF/
              web.xml

I'm trying to create some custom error pages to supersede the default Tomcat ones; generic ones that can be shared by all applications.

I've consulted a few other examples on this and have created an /errors folder, containing the HTML for my error pages, as well as modifying the tomcat/conf/web.xml

<error-page>
   <error-code>404</error-code> <!-- Not Found -->
    <location>/errors/error_404.html</location>
</error-page>

and the tomcat/conf/server.xml

<Valve className="org.apache.catalina.valves.ErrorReportValve"
       error.400="errors/error_400.html"
       error.404="errors/error_404.html" showReport="false"
       showServerInfo="false" />

The issue here is that after deployment and simulating a 404 error, I get something alone the lines of "Page Not Found".

My /errors folder was initially parked under tomcat/ (basically, on the same level as conf/), but I moved it around to try and figure out the cause of this issue.

After moving around the path of my folder, it turns out that the settings in the two XML files(or at least, based on conf/web.xml from what I'm seeing) seems to be pointing towards to conf/webapps/ROOT/errors.

In other words, it works if I add my /errors folder to conf/webapps/ROOT/. This is consistent with the answers I found above.

This is not what I want. I do not want my error folder to be in the ROOT folder, but in another more generic path, as mentioned above under /tomcat

I had even tried something like the full path itself

<error-page>
    <error-code>404</error-code> <!-- Not Found -->
    <location>/apache-tomcat-9.0.37/webapps/errors/error_404.html</location>
</error-page>

and I still get the 'Page not Found' error.

I also had tried, as per one of the answers linked above, starting the path with ./

<error-page>
     <error-code>404</error-code> <!-- Not Found -->
     <location>./errors/error_404.html</location>
</error-page>

and all I got was the server complaining that the URL must start with a forward slash('/').

Trying /../errors/error_404.html didn't really help either.

Where am I going wrong?

1

There are 1 best solutions below

0
Erik On

Welp....I found out where I went wrong.

The conf/server.xml

<Valve className="org.apache.catalina.valves.ErrorReportValve"
       error.400="errors/error_400.html"
       error.404="errors/error_404.html" showReport="false"
       showServerInfo="false" />

error.404 -> errorCode.404

error.400 -> errorCode.400

Not sure if I read the documentation wrong, or the site I was reading it from had the wrong syntax, but once I changed that, my issues were more or less solved. This not only referred to the error pages in the base /tomcat directory as I wanted, but also allowed referencing to other folders, like /tomcat/webapps for example.