submitting a page via a servlet?

F

Flip

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?
 
S

Steve Burrus

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

Flip

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

Sudsy

Flip said:
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.
 
M

Missaka Wijekoon

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
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) (e-mail address removed)
Sr. Software Engineer http://www.villageEdocs.com
VillageEdocs
========================================================================
 
F

Flip

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top