views:

518

answers:

4

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.

A: 

Do you mean status code 100 ?

The API does support sending SC_CONTINUE.

Kieran Tully
When you send 100 Continue to client, you tell the client that it can continue sending request body. After processing request body, you need to send another status, to indicate what happened with your processing. That basically means that you're sending two statuses for one request.
Peter Štibraný
See http://greenbytes.de/tech/webdav/rfc2616.html#use.of.the.100.status for details
Peter Štibraný
I understand how status code 100 is intended to be used (and in fact I'm the one who first posted the correction from 101). It just seems odd that the API allows you to send it if you can't actually use it as intended.
Kieran Tully
+3  A: 

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 not 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.

Julian Reschke
Thanks for answer, Julian. I wanted to use it in combination with Apache Jackrabbit Webdav library to prevent Windows mini-redir client to send huge PUT requests which cannot be processed (as there is missing Authorization header). While I see that there isn't "Expect: 100-continue" header in request, I wanted to try it anyway ... but I didn't know how to code it using servlets :-(
Peter Štibraný
I fixed my question to talk about "100 continue", not 101 ;-) Now I know why I didn't find anything on google when looking for "101 Continue servlet"
Peter Štibraný
+1  A: 

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.

Kieran Tully
"Did you mean to ask How do I send a status code before the request completes?." -- Yes, that was exactly my point. I guess API designers didn't want to add more complexity just for one status code, so we can send it, although not in intented way.
Peter Štibraný
We use Tomcat 5.5 behind Apache HTTP Server (acting as reverse proxy). Thanks for ideas, I'll check it out.
Peter Štibraný
+1  A: 

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.

Michael Slattery
That looks like nice approach within the limits of specification. Good to know about it. Thanks.
Peter Štibraný