Disable all default HTTP error response content in Tomcat?

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

java.sun.com/developer/EJTechTips/2003/t... 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 /myErrorServlet. 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.

I'm not particularly wanting to define static HTML/JSP, because that leads me back to having to configure for any error that I want to not send content. I'm really just looking for a global configuration, if one exists. – Rob Hruska Apr 27 '09 at 20:27 You need to RTFA you can have one servlet handle all of your errors like /myErrorServlet.

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 Apr 29 '09 at 3:35 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 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 Mar 11 '10 at 19:12.

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.

Using print() instead of println() which causes additional new line char. This especially useful if you are sending the empty string, with Content-Length: 0 – mrCoder Sep 28 at 8:55.

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 Apr 27 '09 at 18:02.

As Heikki said, setting the status instead of sendError() causes the Tomcat not touch the response entity/body/payload. If you only want to send the response headers without any entity, like in my case, response. SetStatus(HttpServletResponse.

SC_UNAUTHORIZED); response. SetContentLength(0); does the trick. With Content-Length: 0, the print() will have no effect even if used, like: response.

SetStatus(HttpServletResponse. SC_UNAUTHORIZED); response. SetContentLength(0); response.getWriter().

Print("this string will be ignored due to the above line"); the client receives something like: HTTP/1.1 401 Unauthorized Server: Apache-Coyote/1.1 Content-Type: text/html;charset=utf-8 Content-Length: 0 Date: Wed, 28 Sep 2011 08:59:49 GMT If you want to send some error message, use the setContentLength() with message length (other than zero) or you can leave it the server.

The quick, slightly dirty, but easy way of stopping Tomcat from sending any error body is to call setErrorReportValveClass against the tomcat host, with a custom error report valve which overrides report to do nothing. Ie: public class SecureErrorReportValve extends ErrorReportValve { @Override protected void report(Request request,Response response,Throwable throwable) { } } and set it with: ((StandardHost) tomcat.getHost()). SetErrorReportValveClass(yourErrorValveClassName); If you want to send your message, and just think Tomcat shouldn't mess with it, you want something along the lines of: @Override protected void report(final Request request, final Response response, final Throwable throwable) { String message = response.getMessage(); if (message!

= null) { try { response.getWriter(). Print(message); response.flushBuffer(); } catch (IOException e) { } } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions