Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why getInputStream in a http servlet request isn't getting the datasent by browser HTTP POST action?

Reply
Thread Tools

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

 
 
James
Guest
Posts: n/a
 
      11-25-2005
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!!
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      11-25-2005
James wrote:
> 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).

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
James
Guest
Posts: n/a
 
      11-25-2005
Never mind, I got it.

James wrote:
> 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!!

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-25-2005
On Fri, 25 Nov 2005 15:47:40 -0500, James <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

> int count = is.read(buf,0, available );


see http://mindprod.com/jgloss/readblocking.html
http://mindprod.com/jgloss/readeverything.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Getting servlet context path without a servlet request. stuart.j.wood@gmail.com Java 3 06-06-2006 06:38 AM
seeking servlet "Master" keep getting "Master/servlet/Master" not found. Tomcat 5.0.25 Doug McCann Java 1 08-05-2004 09:16 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Tomcat: POST from one servlet or internal class to another servlet Sean Clarke Java 1 01-07-2004 02:22 PM



Advertisments