views:

778

answers:

3

By default, Tomcat sends some HTML content back to the client if it encounters something like an HTTP 404. I know that via web.xml an <error-page> can be configured to customize this content.

However, I'd just like for Tomcat to not send anything in terms of response content (I'd still like the status code, of course). Is there any way to easily configure this?

I'm trying to avoid A) explicitly sending empty content on the response stream from my Servlet, and B) configuring custom error pages for a whole bunch of HTTP error statuses in my web.xml.

For some background, I'm developing an HTTP API and am controlling my own response content. So for an HTTP 500, for example, I'm populating some XML content on the response containing error information. For situations like an HTTP 404, the HTTP response status is sufficient for clients, and the content tomcat is sending is unnecessary. If there's a different approach, I'm open to hearing it.

Edit: After continued investigation, I still can't find much in the way of a solution. If someone can definitively say this is not possible, or provide a resource with evidence that it will not work, I'll accept that as an answer and try and work around it.

A: 

Why not just configure the <error-page> element with an empty HTML page?

matt b
I mentioned in my question that I would like to avoid configuring error pages (even if they're empty) for a bunch of statuses. If it's my only option then I guess I'll have to - but I'm looking for alternatives.
Rob Hruska
+1  A: 

http://java.sun.com/developer/EJTechTips/2003/tt0114.html in an article that describes how to catch Exceptions or error codes and choose a static page, jsp or Servlet to do the output

From comments:

You can have one servlet handle all of your errors like <error-page><location>/myErrorServlet</location></error-page>. That is less than 100 chars to configure every error. In addition if you are not finding your resource forward to the same servlet. One servlet to customize all of your response.

Clint
I'm not particularly wanting to define static HTML/JSP, because that leads me back to having to configure <error-page> for any error that I want to not send content. I'm really just looking for a global configuration, if one exists.
Rob Hruska
You need to RTFA you can have one _servlet_ handle all of your errors like <error-page><location>/myErrorServlet</location></error-page>. That is less than 100 chars to configure every error. In addition if you are not finding your resource forward to the same servlet. One servlet to customize all of your response.
Clint
Your link is broken, but I ended up finding an example. To be fair, most of the documentation for this usually describes the value for `<location>` as "error page" or "html", which is a bit misleading since it can be a Servlet. But your answer pointed me in the right direction.
Rob Hruska
A: 

If you do not want tomcat to show an error page, then do not use sendError(...). Instead use setStatus(...).

e.g. if you want to give a 405 response, then you do

response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);      
response.getWriter().println("The method " + request.getMethod() + 
   " is not supported by this service.");

Also remember not to throw any Exceptions from your servlet. Instead catch the Exception and, again, set the statusCode your self.

i.e.

protected void service(HttpServletRequest request,
      HttpServletResponse response) throws IOException {
  try {

    // servlet code here, e.g. super.service(request, response);

  } catch (Exception e) {
    // log the error with a timestamp, show the timestamp to the user
    long now = System.currentTimeMillis();
    log("Exception " + now, e);
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    response.getWriter().println("Guru meditation: " + now);
  }
}

of course, if you do not want any content, then just don't write anything to the writer, just set the status.