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

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

问题描述

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

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天全站免登陆