• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Issues with configuring default Error page in Tomcat

 
Ranch Hand
Posts: 99
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

We were trying to incorporate custom error pages for tomcat since server error exposes version details. We have followed some online article and tried to create a general error page and configured in the web.xml. Below is the code that I have included in the tomcat web.xml file


I placed the errorPage.htm inside $tomcat/webapps/$application. When deploying the above, tomcat is unable to display the errorPage.htm configured above in case of 404 exception.

Im able to access the errorPage.htm by navigating to http://localhost:8080/$application/errorPage.htm. But when there is 404 error, it does not return the custom error page configured in the web.xml. I even changed the above configuration to

But still nothing worked. Can you please let me know to fix the same.
Can you help to identify where are the default error pages located in tomcat and how can we fix the above.
Any help on this is much appreciated
 
Saloon Keeper
Posts: 22784
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this, instead:


Your "$application" attempt will not work because JEE (Tomcat) does not support variable substitution in web.xml resource references.

The need for the initial slash in the resource path serves to make it an absolute resource reference. Without it, Tomcat would be using it as a relative resource path and if your URL was complex, the relative base would likely be in the wrong place.

Note that I use the term "resource path". A resource path is not the same thing as a URL. Resources are things that the webapp uses to construct responses to URLs - in the case of a 404 error that logic is part of the Tomcat built-in "default servlet" that gets run when Tomcat cannot find a resource based on the incoming URL.

The most important thing to note about a resource path - other that that it's not a URL - is that it should be specified as an absolute path (hence the initial slash). From the webapp's internal point of view, the root of the resource tree is the root of the WAR. It doesn't know or care about the webapp context ("$application").
 
Varshini Priya
Ranch Hand
Posts: 99
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim, for the below response.  Im in the process of performing tomcat hardening and as part of it.  I do not want our application to display tomcat version details etc at the time of error.  Hence wanted the application to pick up the custom error pages. Even after including the  initial '/' in the resource path, Im unable to get the desired output. The application is not navigating to the error page.html.  In other words, where do I need to place the resource i.e. the error.html  page once I provided the path in the web.xml.

Can you also let me know the path in tomcat where the default error page exists ?

 
Tim Holloway
Saloon Keeper
Posts: 22784
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When Tomcat dispatches a URL to a web application, it looks in that webapp's web.xml data (both the /WEB-INF/web.xml and any extra info that annotations added). There it attempts to match the URL path to one of the web.xml servlet mappings (if there are any). If no match can be made. an attempt is made to find a JSP and to compile the JSP if it hasn't been compiled (and cached) already.

If neither of the above works, then control passes to Tomcat's default servlet. The default servlet is built into Tomcat itself and it attempts to resolve URLs into static resource paths. If a static resource (such as an image, JavaScript or CSS) is found, the default servlet copies that resource to the response stream.

If the resource is a directory and directory listing is enabled, there is logic in the default servlet that will build a directory display webpage and send it back.

If none of the above works, the default servlet creates the standard Tomcat "404" page.

Please note that nothing in the JEE specs requires that a servlet should do the above, so coding logic assuming that there's a default servlet is not recommended. Also, modifying the default servlet isn't something I'd recommend. It requires a higher skill level than general app coding and may break when newer Tomcat releases have to be implemented. Not the sort of job you should hand to whichever junior programmer happens to be idle.

However, the web.xml entry I showed you should be sufficient. If you are getting the standard 404 page anyway, check the exact spelling and capitalization of your web.xml element as well as where you placed it within web.xml. Make sure that the proper copy of web.xml is in the WAR that Tomcat deploys. And very definitely make sure that Tomcat has file read access rights to the error page file.

If you still have problems, carefully check both the catalina.out log and the localhost log for suspicious messages.

And, incidentally, when I see a URL that ends with ".htm", I know that there's a fair chance that the app is running under Windows, so I know which tools are most likely to be able to exploit it.
 
Nothing up my sleeve ... and ... presto! A tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic