3

First off, let me say that I am not a Java programmer or very familiar with Tomcat so please bear with me :) I am working with a SaaS application running on Apache Tomcat 5.0.9. I do not have access to the server configuration files nor can I add .jsp files.

The situation I am in is I have a script that is running in a Mozilla Rhino environment and I have access to the following two objects:

org.apache.coyote.tomcat5.CoyoteRequestFacade org.apache.coyote.tomcat5.CoyoteResponseFacade

I need to set the status to SC_NOT_FOUND (404) and provide a custom message. The problem is, no matter what I do, if the status is 404, I get an "ugly" 404 error message as set by the vendor. I have tried using "sendError", "setStatus", I've tried getting the dispatcher and redirecting to another page while also setting the status to 404, I've tried a wrapper but not sure what I'm doing, and I've seen information about filters but do not know how to apply that in this situation or even if that is the right approach.

Again, it seems that as soon as the 404 is set, the web server serves up it's own page. I'm assuming this is an "error page declaration" that I cannot override. I have read through this question How to get the message in a custom error page (Tomcat)? (and many others) but I do not understand this enough to translate the information to my particular situation.

So, is it possible to "override" an error page declaration programmatically? Or, how can I return a 404 error and a custom error page given the limitations of the environment I am in? I would settle, at this point, for just plain text - doesn't have to be fancy. I just need some way to use the 404 status and display a "user friendly" message.

Any help would be greatly appreciated!

P.S. I should mention that the reason for this approach is that the page in question will be consumed (potentially) by both a search engine and humans. I need the 404 so that the search engine removes the page from its cache and I need the "pretty" message to tell the human vistor what steps they need to take due to the no longer available page.

Community
  • 1
  • 1

1 Answers1

2

Tomcat is interpreting your 404 status and displaying the standard error page with your error message or stacktrace.

If using response.sendError(404, "Your error message here") isn't working for you then you need to look at using custom error pages - http://wiki.apache.org/tomcat/FAQ/Miscellaneous#Q6, but this relies on the fact that you can amend the web.xml - seomthing you said you are unable to do.

Now you could return an ok HTTP status code (200), and the pretty formatted error content to be displayed

if the client consuming the response is just a browser with a human attached, then they will be none the wiser.

On the other hand, if the client consuming it is a machine of some sort (script, another program etc) and they are performing logic based upon the response code then this is not a good solution for you as by returning a 200 status code, you are saying everything went ok (rather than an error code - 4xx or 5xx)

Chris White
  • 29,949
  • 4
  • 71
  • 93
  • Thank you for that! I should have mentioned that the 404 will be consumed by both a machine and people which is why I need both. I need the 404 so the machine removes the page from it's cache but if a human reads it, I need a "pretty" message. I will update my question. – Andrew Kincaid Mar 25 '12 at 16:24
  • If you can't amend web.xml, i don't think there is any way you can replace the standard tomcat error page – Chris White Mar 25 '12 at 16:26