submitting a page via a servlet?

Discussion in 'Java' started by Flip, May 14, 2004.

  1. Flip

    Flip Guest

    I tried submitting a request via HttpURLConnection, and a weird thing
    happened. I didn't get any activity on the server until I ran the line of
    code that opened the input stream (InputStream rawInStream =
    conn.getInputStream();). Why is that?

    Here's the code I was trying

    // URL must use the http protocol!
    HttpURLConnection conn = ( HttpURLConnection )
    url.openConnection();
    conn.setRequestMethod( "POST" );
    conn.setAllowUserInteraction( false ); // you may not ask the user
    conn.setDoOutput( true ); // we want to send things
    // the Content-type should be default, but we set it anyway
    conn.setRequestProperty( "Content-type",
    "application/x-www-form-urlencoded" );

    // the content-length should not be necessary, but we're cautious
    conn.setRequestProperty( "Content-length", Integer.toString(
    body.length() ) );

    // get the output stream to POST our form data
    OutputStream rawOutStream = conn.getOutputStream();
    PrintWriter pw = new PrintWriter( rawOutStream );

    pw.print( body ); // here we "send" our body!
    pw.flush();
    pw.close();

    // get the input stream for reading the reply
    // IMPORTANT! Your body will not get transmitted if you get the
    // InputStream before completely writing out your output first!
    InputStream rawInStream = conn.getInputStream();

    And I got no activity on the server (no System.err.println() ) in my servlet
    until I rad the getInputStream(). I thought I would get the servlet running
    as soon as I ran the flush or even the close. Why wouldn't it do anything?
     
    Flip, May 14, 2004
    #1
    1. Advertising

  2. Flip

    Steve Burrus Guest

    Just a "stab" at what's wrong. Did you include the usual "public void
    doGet('.....') method to start or initialize the servlet??

    Flip wrote:

    > I tried submitting a request via HttpURLConnection, and a weird thing
    > happened. I didn't get any activity on the server until I ran the line of
    > code that opened the input stream (InputStream rawInStream =
    > conn.getInputStream();). Why is that?
    >
    > Here's the code I was trying
    >
    > // URL must use the http protocol!
    > HttpURLConnection conn = ( HttpURLConnection )
    > url.openConnection();
    > conn.setRequestMethod( "POST" );
    > conn.setAllowUserInteraction( false ); // you may not ask the user
    > conn.setDoOutput( true ); // we want to send things
    > // the Content-type should be default, but we set it anyway
    > conn.setRequestProperty( "Content-type",
    > "application/x-www-form-urlencoded" );
    >
    > // the content-length should not be necessary, but we're cautious
    > conn.setRequestProperty( "Content-length", Integer.toString(
    > body.length() ) );
    >
    > // get the output stream to POST our form data
    > OutputStream rawOutStream = conn.getOutputStream();
    > PrintWriter pw = new PrintWriter( rawOutStream );
    >
    > pw.print( body ); // here we "send" our body!
    > pw.flush();
    > pw.close();
    >
    > // get the input stream for reading the reply
    > // IMPORTANT! Your body will not get transmitted if you get the
    > // InputStream before completely writing out your output first!
    > InputStream rawInStream = conn.getInputStream();
    >
    > And I got no activity on the server (no System.err.println() ) in my servlet
    > until I rad the getInputStream(). I thought I would get the servlet running
    > as soon as I ran the flush or even the close. Why wouldn't it do anything?
    >
    >
     
    Steve Burrus, May 14, 2004
    #2
    1. Advertising

  3. Flip

    Flip Guest

    > Just a "stab" at what's wrong. Did you include the usual "public void
    > doGet('.....') method to start or initialize the servlet??

    Yup, the servlet works properly when I do the getInputStream command. It's
    even coded for POST requests too.
     
    Flip, May 14, 2004
    #3
  4. Flip

    Sudsy Guest

    Flip wrote:
    > I tried submitting a request via HttpURLConnection, and a weird thing
    > happened. I didn't get any activity on the server until I ran the line of
    > code that opened the input stream (InputStream rawInStream =
    > conn.getInputStream();). Why is that?

    <snip>

    Without digging into the source, I can't tell you. What I HAVE noticed
    is that things don't always work the way you might think they should.
    The order of the method invocations for making the request a POST rather
    than a GET is a prime example. It's counter-intuitive.
    But does it really matter at end-of-day?
    IOW, as long as you know how to make it work then what does it matter
    what goes on "under the hood"? I've learned to accept it and live with
    it.
     
    Sudsy, May 14, 2004
    #4
  5. It kind of makes sense if you think of the HTTP protocol itself: you
    need an output stream to write (POST) the data, but having submitted the
    data, you need a response from the server (an input stream). Did the
    server issue a 200? A 401? How about a 404? Was there a redirect?
    Essentially the transaction is not complete until data is read back from
    the connection. So what happens first or the order in which events
    occur may be immaterial if you are using a high level class like
    HttpURLConnection.

    For the level of control you are speaking of, perhaps a socket is better.

    -Misk

    Flip wrote:
    > I tried submitting a request via HttpURLConnection, and a weird thing
    > happened. I didn't get any activity on the server until I ran the line of
    > code that opened the input stream (InputStream rawInStream =
    > conn.getInputStream();). Why is that?
    >
    > Here's the code I was trying
    >
    > // URL must use the http protocol!
    > HttpURLConnection conn = ( HttpURLConnection )
    > url.openConnection();
    > conn.setRequestMethod( "POST" );
    > conn.setAllowUserInteraction( false ); // you may not ask the user
    > conn.setDoOutput( true ); // we want to send things
    > // the Content-type should be default, but we set it anyway
    > conn.setRequestProperty( "Content-type",
    > "application/x-www-form-urlencoded" );
    >
    > // the content-length should not be necessary, but we're cautious
    > conn.setRequestProperty( "Content-length", Integer.toString(
    > body.length() ) );
    >
    > // get the output stream to POST our form data
    > OutputStream rawOutStream = conn.getOutputStream();
    > PrintWriter pw = new PrintWriter( rawOutStream );
    >
    > pw.print( body ); // here we "send" our body!
    > pw.flush();
    > pw.close();
    >
    > // get the input stream for reading the reply
    > // IMPORTANT! Your body will not get transmitted if you get the
    > // InputStream before completely writing out your output first!
    > InputStream rawInStream = conn.getInputStream();
    >
    > And I got no activity on the server (no System.err.println() ) in my servlet
    > until I rad the getInputStream(). I thought I would get the servlet running
    > as soon as I ran the flush or even the close. Why wouldn't it do anything?
    >
    >



    --
    ========================================================================
    Missaka Wijekoon (a.k.a. Misk)
    Sr. Software Engineer http://www.villageEdocs.com
    VillageEdocs
    ========================================================================
     
    Missaka Wijekoon, May 15, 2004
    #5
  6. Flip

    Flip Guest

    > It kind of makes sense if you think of the HTTP protocol itself: you
    It kind of does, but only after digging, and digging, and reading and
    reading...

    > server issue a 200? A 401? How about a 404? Was there a redirect?

    That's where I was looking at HttpURLConnection cause I can look at the
    resonsecode from that. I started coding it up with URLConnection, but
    couldn't look at the server response. But with the HttpURLConnection, I was
    getting a 200 but nothing on the server. :<

    > Essentially the transaction is not complete until data is read back from

    That's kind of what I was thinking. In order to complete the transaction
    you have to be willing to stick around for the conclusion I guess.

    > For the level of control you are speaking of, perhaps a socket is better.

    DOH! I was hoping to use higher up classes. But like you said, I just have
    to deal with that.

    Thanks.
     
    Flip, May 17, 2004
    #6
    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. Jo Inferis
    Replies:
    7
    Views:
    2,154
    Marina
    May 25, 2005
  2. circuit_breaker
    Replies:
    2
    Views:
    2,017
    Jack Jia
    Apr 4, 2004
  3. ILPTAB
    Replies:
    21
    Views:
    928
    ILPTAB
    Oct 9, 2007
  4. CJM
    Replies:
    4
    Views:
    129
  5. Richard Dixson

    Problem submitting form via JS

    Richard Dixson, Dec 13, 2004, in forum: Javascript
    Replies:
    1
    Views:
    130
    Martin Honnen
    Dec 13, 2004
Loading...

Share This Page