禁用Tomcat中所有默认的HTTP错误响应内容 [英] Disable all default HTTP error response content in Tomcat

查看:23
本文介绍了禁用Tomcat中所有默认的HTTP错误响应内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Tomcat 会在遇到类似 HTTP 404 之类的内容时将一些 HTML 内容发送回客户端.我知道通过 web.xml 可以配置来自定义此内容.

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.

但是,我只是希望 Tomcat 在响应内容方面不发送任何内容(当然,我仍然喜欢状态代码).有什么方法可以轻松配置它吗?

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?

我试图避免 A) 在我的 Servlet 的响应流上显式发送空内容,以及 B) 在我的 web.xml 中为一大堆 HTTP 错误状态配置自定义错误页面.

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.

对于某些背景,我正在开发一个 HTTP API 并控制我自己的响应内容.例如,对于 HTTP 500,我在包含错误信息的响应中填充一些 XML 内容.对于像 HTTP 404 这样的情况,客户端的 HTTP 响应状态就足够了,而 tomcat 正在发送的内容是不必要的.如果有不同的方法,我愿意倾听.

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.

经过不断的调查,我仍然找不到太多的解决办法.如果有人可以明确地说这是不可能的,或者提供资源证明它行不通,我会接受它作为答案并尝试解决它.​​

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.

推荐答案

如果不想让 tomcat 显示错误页面,那么不要使用 sendError(...).而是使用 setStatus(...).

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

例如如果你想给出 405 响应,那么你做

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.");

还要记住不要从您的 servlet 中抛出任何异常.而是捕获异常,并再次设置自己的 statusCode.

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

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.

这篇关于禁用Tomcat中所有默认的HTTP错误响应内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆