-----------------------------
Please read the FAQ!
<http://java.apache.org/faq/>
-----------------------------
Solomon Douglas wrote:
>
> This is a promising idea. But, if I go "InputStream in =
> request.getInputStream();" then I find that in.available() == 0.
> Doesn't this mean that there is in fact no input waiting to be read?
> (I'm actually quite confused about how the ServletRequest works. If
> ServletRequest provides all these methods (such as getParameter) which
> require it to have already looked at the input stream, then what data
> is left to be read when I call getInputStream?
>
> I was simply planning to call in.skip(in.available()) to finish up
> with the request input so that the response will work, but if
> in.available() == 0 then that obviously won't do much for me.
>
The available() method only tells you how many bytes you are guaranteed to be
able to read without blocking. A value of zero indicates that the next byte you
read from the stream will go down to the operating system to read from the
underlying socket, which *may* cause the thread to block until more data is
available (it may actually be buffered at the OS or JVM level). For servlets,
this is not a particularly useful call.
If you want to read the entire input stream, you should call
request.getContentLength() to see how many bytes the browser sent. If you get a
non-negative response, then that is how many bytes you need to read. If the
response is negative, that means the client did not specify a content length, so
you should just read until you get an EOF indication on the input stream.
With only one exception, the servlet engine does not read the content bytes of
the input request to fill out all the values for the request object to return.
Instead, it reads only the HTTP headers that preceeded the data content. (See
the HTTP specifications at http://www.w3c.org to find out how the HTTP protocol
works). The one exception is the following:
* You are doing a POST request (usually a form submit)
* The content type is application/x-form-urlencoded
(this is the default on a <FORM> submit unless you declare
something different
* Your servlet calls one of the getParameter() family of methods.
If all these conditions are true, the servlet engine will read the content of
the input stream to extract the submitted form parameters. If you then go get
an input stream and try to read it, there will be no bytes left.
>
> Solomon
Craig McClanahan
--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Archives and Other: <http://java.apache.org/main/mail.html>
Problems?: [EMAIL PROTECTED]