[Advanced-java] Sending a Post from an applet to a servlet
Martin Cooper
martin.cooper at tumbleweed.com
Fri Apr 11 18:29:10 2003
> -----Original Message-----
> From: Steven J. Owens [mailto:puffmail@darksleep.com]
> Sent: Friday, April 11, 2003 2:58 AM
> To: Lorin Davis
> Cc: advanced-java@lists.xcf.berkeley.edu
> Subject: Re: [Advanced-java] Sending a Post from an applet to
> a servlet
>
>
> On Thu, Apr 10, 2003 at 04:10:21PM -0600, Lorin Davis wrote:
> > I have an applet that opens a URLConnection and a servlet that
> > receives this connection in the doGet () I'm trying to change this
> > to go through the doPost () with a parameter added to the post.
>
> When posting, in general I suggest reading this article first
> (though it doesn't address your specific problem):
>
> http://www.javaworld.com/javaworld/jw-03-2001/jw-0323-traps.html
>
> > The problem is that we want to compress our tokenXML (as sometimes
> > it can be very long) before sending it to the servlet and not send
> > it as part of the URL instead send it as a parameter of a post.
> >
> > My understanding is you can send any type of data to a post method
> > as a parameter but I cannot figure out how to do this from an applet
> > yet any help would be much appreciated.
>
> This is not as easy as it should be.
>
> You should very strongly consider using multipart/form-data (RFC
> 2388) encoding for XML payloads, particularly for binary, compressed
> XML payloads. To do this, I strongly suggest you use the Jakarta
> HttpUnit or Jakarta HttpClient, or the Innovation.ch HTTPClient.
>
> http://www.ietf.org/rfc/rfc2388.txt
>
> I know for a fact that both HttpUnit and the Innovation.ch
> HTTPClient (not to be confused with the jakarta HttpClient project,
> which somebody informed me of when I posted a message similar to this
> one in advanced-servlets the other day :-) support
> multipart/form-data mime
> encoding. You have to dig for it in HttpUnit - it's in there, but the
> docs didn't exactly point it out when I looked into this about six or
> eight months ago (I ended up writing my own, but that's another
> story).
(I'm here too, Steven! ;)
Jakarta Commons HttpClient also supports multipart/form-data content.
http://jakarta.apache.org/commons/httpclient/
>
> This will also require a little more coding on the server side,
> there are a few libraries out there to support decoding
> multipart/form-data uploads. The one I'm most familiar with is
> (surprise surprise) Jason Hunter's, which is available at servlets.com
> for download.
>
> Note that the license requires you to buy a copy of Jason's
> excellent book, _Java Servlet Programming_ to use the
> multipart/form-data parser for commercial use). However, I tripped
> across another parser on the web, the other day, when looking for
> something else (of course I didn't bother to note that one down).
There's also Jakarta Commons FileUpload, which doesn't require you to buy
anything. ;-)
http://jakarta.apache.org/commons/fileupload/
--
Martin Cooper
>
> You may also want to look into how SOAP typically handles its
> payloads when using HTTP for the transport layer, though it's likely
> the SOAP client kit may be too bulky or require too many support APIs
> to be used in an applet (though I'm sure there are people out there
> trying to make an applet-appropriate SOAP client).
>
>
> I was going to skip over this next bit, but then I decided that
> since we're talking about POST, and it's often a topic that confuses
> and annoys folks...
>
> To start, when posting, in general I suggest reading this article
> first (though it doesn't address your specific problem):
>
> http://www.javaworld.com/javaworld/jw-03-2001/jw-0323-traps.html
>
> Actually POSTing with parameters is, however, not discussed
> in that article.
>
> Again, it's not as easy as it should be. But you can code up the
> parameter yourself and get it going. It's not painless, but it's not
> *that* hard. Putting together a more generic solution would require a
> little work, but if you just want to do a one-off, no problem. It
> works this way.
>
> An HTTP POST is pretty much like a GET. The two transactions
> look very much the same if you watch them with a packet sniffer (*).
>
> (* Using a packet sniffer to watch your browser (or in this case
> applet) talk to your server is something which I EXTREMELY strongly
> recommend to anybody who is working with web protocols - not only is
> it an excellent education in how HTTP and browsers work, it's also an
> invaluable debugging tool.)
>
> There's one big difference. A normal GET will request a URL with
> the parameters appended to the URL in an encoded format. We've all
> seen GET URLs. A POST will put the parameter stuff - in the same
> exact format, everything that you would normally see after the
> question mark on a GET URL in your browser's Location: or Address:
> window - at the end of the request, after the headers.
>
> Your problem is, you have to assemble that string by hand,
> instead of being able to just say "do a post and here are the
> parameters".
>
> Fortunately, the only really obnoxious bit is done for you, by
> java.net.URLEncoder. URL encoding is the steps necessary to make the
> parameter data safe to be included in a URL, for example making sure
> that there are no spaces in the data (by replacing them with plus (+)
> signs).
>
> From the javadocs for URLEncoder:
>
> For example using UTF-8 as the encoding scheme the string "The
> string @foo-bar" would get converted to
> "The+string+%C3%BC%40foo-bar"
> because in UTF-8 the character is encoded as two bytes
> C3 (hex) and
> BC (hex), and the character @ is encoded as one byte 40 (hex).
>
> The only part left for you is to concatenate the
> parameter name and
> parameter data with an equals sign in between them:
>
> foo=bar
>
> And then concatenate the parameters together with an &
> between them:
>
> foo=bar&baz=xyzzy
>
> Then you post and write this string to the URL.
>
>
> Steven J. Owens
> puff@darksleep.com
>
> "I'm going to make broad, sweeping generalizations and strong,
> declarative statements, because otherwise I'll be here all night and
> this document will be four times longer and much less fun to read.
> Take it all with a grain of salt." - Me at http://darksleep.com
>
> _______________________________________________
> Advanced-java mailing list
> Advanced-java@lists.xcf.berkeley.edu
> http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
>