如何在 Java servlet 容器上指定 Http 请求超时参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1414795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:50:13  来源:igfitidea点击:

How to specify Http Request timeout parameter on Java servlet container

javaservletstimeouthttprequestcontainers

提问by Ittai

I'm trying to understand where I can configure a request timeout for all requests arriving to a servlet of mine (or all of my servlets)? Is that, as I think, a container property? Also, how does this affect different browsers? Do they all comply to the parameter the container dictates? Or maybe the request timeout time isn't even something I can control and each browser decides on this on its own? (Just to be clear I'm not talking about session timeout)

我想了解在哪里可以为到达我的 servlet(或我的所有 servlet)的所有请求配置请求超时?在我看来,那是容器属性吗?另外,这如何影响不同的浏览器?它们是否都符合容器规定的参数?或者请求超时时间甚至不是我可以控制的,每个浏览器都自行决定?(只是要清楚我不是在谈论会话超时)

采纳答案by Vinay Sajip

The timeout from a client (i.e. how long it waits for a response to an HTTP request) is determined at the client. For IE, see this, and for Firefox see this.

客户端的超时时间(即等待对 HTTP 请求的响应的时间)在客户端确定。对于 IE,请参阅,对于 Firefox,请参阅

You can't control this timeout from the server.

您无法从服务器控制此超时。

回答by David Tinker

You cannot control the client timeout from the server. However you may be able to send data back to the client every now and then while your long running operation is busy. This will prevent the client from timing out and can be used to display progress to the user etc. Write data to the OutputStream or Writer obtained from the response and call flush to send partial data to the client.

您无法从服务器控制客户端超时。但是,您可以在长时间运行的操作繁忙时不时将数据发送回客户端。这将防止客户端超时,并可用于向用户显示进度等。将数据写入从响应中获得的 OutputStream 或 Writer,并调用flush 将部分数据发送到客户端。

回答by ZZ Coder

Even though you can't control client timeout, you can make server very impatient :) For example, on Tomcat, you can do this in your connector,

即使您无法控制客户端超时,您也可以让服务器非常不耐烦:) 例如,在 Tomcat 上,您可以在连接器中执行此操作,

<Connector port="8080"  
  ...
  connectionTimeout ="5000"
  disableUploadTimeout="false" />

This makes server only wait 5 seconds and close the connection. Browser will get a connection closed error. You can treat it the same as timeout in client.

这使得服务器只等待 5 秒并关闭连接。浏览器将收到连接关闭错误。您可以将其视为客户端中的超时。

Of course, this only works if the timeout is caused by the server, not connectivity issues between browser and server.

当然,这仅在超时是由服务器引起的情况下才有效,而不是浏览器和服务器之间的连接问题。