默认情况下,Tomcat发送某些内容回到客户,如果遇到的东西喜欢HTTP404.我知道,通过 web.xml 一个 <error-page> 能配置 要定制内容。

然而,我只想对Tomcat到 不送任何东西 在反应方面的内容(我还是喜欢的地位代码,然)。是否有任何方式容易配置的这个?

我试图避免一)明确发送空内容的响应流离我Servlet,并B)配置定义错误的网页为一大堆HTTP错误的状态在我的 web.xml.

对于某些背景,我的发展中HTTP API和我控制我自己的响应的内容。因此,对于HTTP500例如,我填充有一些XML上的内容应含有错误信息。对于这样的情况下HTTP404,HTTP响应的状态是足够为客户和内容tomcat是发送是不必要的。如果有一个不同的方法,我听到它。

编辑: 后续调查,我还是不能找到多方式的一个解决方案。如果某人可以明确地说,这是不可能的,或者提供一个资源有证据表明,它不会的工作,我会接受这一答案和尝试和工作。

有帮助吗?

解决方案

如果您不希望Tomcat能够显示错误页面,则不要使用sendError(...)。而是使用setStatus(...)。

e.g。如果你想给一个405响应,那么您

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

另外切记不要从你的servlet抛出任何异常。相反,捕获异常,并再次设定的StatusCode你的自我。

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);
  }
}

当然,如果你不希望任何内容,那么就不要写什么的作家,刚刚设置的状态。

其他提示

虽然这并不完全响应的问题“不发送任何东西”的声明,并在克莱夫·埃文斯的浪潮回答,我发现,在Tomcat中,你可以使那些过于冗长文本从错误页面消失,而无需创建一个自定义ErrorReportValve。

您可以在您的 “server.xml中” 通过2个PARAMS “showReport” 和 “showServerInfo” 完成这个定制ErrorReportValve

<Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" />

链接到正式文档

为我工作在Tomcat 7.0.55,我在Tomcat 7.0.47没有工作(我想是因为一些报告以下链接的 http://www.mail-archive.com/users@tomcat.apache.org/msg113856.html

发送任何错误的身体停止Tomcat的快速,稍脏的,但简单的方法是调用setErrorReportValveClass对tomcat的主机,与覆盖报告什么也不做一个自定义的错误报告阀。即:

public class SecureErrorReportValve extends ErrorReportValve {

@Override
protected void report(Request request,Response response,Throwable throwable) {
}

}

和与设置它:

  ((StandardHost) tomcat.getHost()).setErrorReportValveClass(yourErrorValveClassName);

如果您要发送您的消息,只是觉得Tomcat的不应该惹它,你想沿着线的东西:

@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.finishResponse();
        } catch (IOException e) {
        }
    }
}

正如所述海基,设置状态而不是sendError()导致的Tomcat不触及响应实体/体/有效载荷。

如果您只想发送响应报头没有任何实体,就像在我的情况,

response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentLength(0);

的伎俩。与Content-Length: 0,所述print()将没有任何效果,即使使用这样的:

response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentLength(0);
response.getWriter().print("this string will be ignored due to the above line");

客户端接收喜欢的东西:

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

如果要发送一些错误消息,使用与setContentLength()消息长度(除零之外),也可以留待服务器

虽然这是Servlet规格符合标准,出于安全原因,我不想tomcat或任何其他Servlet容器发送错误信息。我挣扎了这一点。后搜索和努力,解决方案可以归纳为:

  1. 正如其他人所提到的,不用 sendError(), 使用 setStatus() 而不是
  2. 框架,例如:春天安全使用 sendError() 虽然...
  3. 写一个 Filter
    a.重呼吁 sendError()setStatus()
    b.刷新反应结束,以防止容器从进一步修改的反应

一个小小的例servlet过滤器这样做可以在这里找到.

为什么不只是一个空的HTML页面配置<error-page>元素?

尽管这个问题是有点老了,我就遇到了这个问题了。首先,Tomcat的行为是绝对正确的。这是每Servlet规范。一个人不应该改变Tomcat的对规范行为。作为海基Vesalainen和mrCoder提到的,使用setStatussetStatus

要敬启者,我曾提出一个 Tomcat来提高sendError的文档。

配置 <error-page> 元素 web.xml

编辑 $CATALINA_HOME/conf/web.xml,在文末添加以下 <error-page>, ,保存和重新启动tomcat

<web-app>

...
...
...

    <error-page>
        <error-code>404</error-code>
        <location>/404.html</location>
    </error-page>

    <error-page>
        <error-code>500</error-code>
        <location>/500.html</location>
    </error-page>

    <error-page>
        <error-code>400</error-code>
        <location>/400.html</location>
    </error-page>

</web-app>
  • 它的伟大工程,因为我希望尽管没有实际上创造了一个有效途径,指定的那些 location 值(例如 /400.html)

之前

enter image description here

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top