Hell Gurus,
I have a stupid question here need your mighty hands.
I wrote a very simple Servlet to test the POST data function:
==
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class Test extends javax.servlet.http.HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse resp) {
try {
BufferedInputStream is = new
BufferedInputStream(req.getInputStream());
int available = is.available();
byte[] buf= new byte[8192];
int count = is.read(buf,0, available );
ServletOutputStream out = resp.getOutputStream();
out.println( available + " " + count);
} catch (Exception e ) {}
}
}
==
And I use the following HTML page to send the request to this servlet:
==
<html>
<body>
<form ACTION="http://localhost:8080/Test" METHOD="POST">
<TEXTAREA ROWS=5 COLS=72 NAME=Comments></TEXTAREA>
<input type="Submit">
</form>
</body>
</html>
==
The problem is, the servlet always says that there is no data available
in the inputstream. I have tried everything I know but it stays the
same. I also tried to use enctype="multipart/form-data" on the browser
side...
Can anyone please point me to a correct direction? I have spent hours
over internet and news groups trying to find an answer...
Note: the test was done on Tomcat 5.0.28 + JDK 1.4.2
Thanks a lot in advance!!
John C. Bollinger - 25 Nov 2005 21:35 GMT
> Hell Gurus,
>
[quoted text clipped - 37 lines]
> same. I also tried to use enctype="multipart/form-data" on the browser
> side...
Chances are very good that the problem is in your use of available(), a
method whose usefulness is extremely limited, but which is misunderstood
and misapplied with disturbing regularity. In particular, available()
tells only how many bytes can be read from the stream *without
blocking*, *right now*. That is only of interest if you are trying to
avoid blocking, and not necessarily so much use even then. Furthermore,
without looking at the source I speculate that BufferedInputStream's
available() method might be based on the number of bytes available from
the /buffer/, which might not be filled the first time until a read is
attempted on the stream.
To read the input stream simply read it until it reports end-of-stream,
processing it either as you go or after you have all the data, as
appropriate. You may wrap the stream in a buffered one if you wish,
though if you need to read everything from the stream before you do
anything with it then doing your own buffering may be at least as
efficient (and you're halfway doing that already anyway).

Signature
John Bollinger
jobollin@indiana.edu
James - 25 Nov 2005 21:50 GMT
Never mind, I got it.
> Hell Gurus,
>
[quoted text clipped - 44 lines]
>
> Thanks a lot in advance!!
Roedy Green - 25 Nov 2005 23:17 GMT
> int count = is.read(buf,0, available );
see http://mindprod.com/jgloss/readblocking.html
http://mindprod.com/jgloss/readeverything.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.