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

Discussion in 'Java' started by James, Nov 25, 2005.

  1. James

    James Guest

    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!!
     
    James, Nov 25, 2005
    #1
    1. Advertising

  2. Re: Why getInputStream in a http servlet request isn't getting thedata sent by browser HTTP POST action?

    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
     
    John C. Bollinger, Nov 25, 2005
    #2
    1. Advertising

  3. James

    James Guest

    Re: Why getInputStream in a http servlet request isn't getting thedata sent by browser HTTP POST action?

    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!!
     
    James, Nov 25, 2005
    #3
  4. James

    Roedy Green Guest

    Roedy Green, Nov 25, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    2,017
    Jack Jia
    Apr 4, 2004
  2. Replies:
    3
    Views:
    29,131
    Stuart Wood
    Jun 6, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,992
    Smokey Grindel
    Dec 2, 2006
  4. Hiten
    Replies:
    0
    Views:
    398
    Hiten
    Apr 30, 2008
  5. Hiten
    Replies:
    0
    Views:
    394
    Hiten
    Apr 30, 2008
Loading...

Share This Page