-->

Sending 100 Continue using Java Servlet API

2020-03-28 02:49发布

问题:

Is it possible to send "100 Continue" HTTP status code, and then later some other status code after processing entire request using Java Servlet API (HttpServletResponse)?

I can't find any definitive "No" answer, although API doesn't seem to support it.

回答1:

I assume you mean "100 Continue".

The answer is: no, you can't (at least not the way it's intended, as provisional response). In general, the servlet engine will do it automatically, when the request requires it. Of course this makes it impossibe for the servlet to prevent sending the 100 status -- this issue is a known problem in the Servlet API, and has been known for what feels like eons now.



回答2:

I know that Jetty will wait until getReader() or getInputStream() is called before it sends a 100. I think this is the behavior you are looking for. I don't know what Tomcat does.



回答3:

Did you mean to ask How do I send a status code before the complete request is received, to interrupt an in-progress request due to a missing header field? It seems that's not possible with standard servlets.

What server are you using?

Some server's servlet extensions may allow this, e.g. Tomcat's Comet servlet might send EventType.BEGIN once the headers are available to process, which may allow you to interrupt a PUT that doesn't have the correct authentication.

Alternatively your server might have a plugin to reject requests based on headers.



回答4:

Do you mean status code 100 ?

The API does support sending SC_CONTINUE.