Why getInputStream in a http servlet request isn't getting the datasent by browser HTTP POST action?

J

James

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!!
 
J

John C. Bollinger

James said:
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...

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top