Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > submitting a page via a servlet?

Reply
Thread Tools

submitting a page via a servlet?

 
 
Flip
Guest
Posts: n/a
 
      05-14-2004
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?


 
Reply With Quote
 
 
 
 
Steve Burrus
Guest
Posts: n/a
 
      05-14-2004
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?
>
>


 
Reply With Quote
 
 
 
 
Flip
Guest
Posts: n/a
 
      05-14-2004
> 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.


 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      05-14-2004
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.

 
Reply With Quote
 
Missaka Wijekoon
Guest
Posts: n/a
 
      05-15-2004
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) http://www.velocityreviews.com/forums/(E-Mail Removed)
Sr. Software Engineer http://www.villageEdocs.com
VillageEdocs
================================================== ======================
 
Reply With Quote
 
Flip
Guest
Posts: n/a
 
      05-17-2004
> 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.


 
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
Submitting form via xmlhttp request Steve Swift Javascript 2 01-31-2008 11:44 AM
Disable Validation when submitting via JScript? Jo Inferis ASP .Net 7 05-25-2005 08:11 PM
submitting content page using master page =?Utf-8?B?aGlfbmVoYQ==?= ASP .Net 0 02-16-2005 12:23 PM
Problem submitting form via JS Richard Dixson Javascript 1 12-13-2004 03:34 PM
Submitting multiple record IDs via a form. CJM ASP General 4 10-23-2003 02:58 PM



Advertisments