[Advanced-java] Sending a Post from an applet to a servlet

Gorokhov, Mark V. Mark.Gorokhov at celera.com
Fri Apr 11 14:00:18 2003


This is a working code (I removed our special cases). 

    private void postParams(URLConnection uc) throws IOException
    {
        // http://www.geocrawler.com/archives/3/188/1999/7/0/2464487/
        // Problem:
        //    Applet within the Netscape browser cannot submit "POST"
        //    parameters to a servlet URL.
        // Fix:
        //    The problem is in the Content-type linked with a
HttpRequest 
        //    sent by different browsers. Netscape browser set
content-type 
        //    (A) "multipart/form-data", other browsers  set
content-type 
        //    (B) "application/x-www-form-urlencoded". However, Jserv
requires 
        //    the content-type (B) in order to decode "POST" 
        //    (see org.apache.jserv.JServConnection.java, line 768).
        uc.setRequestProperty("Content-type",
                              "application/x-www-form-urlencoded");
        uc.setDoOutput(true);
        OutputStream os = uc.getOutputStream();
        if (bSendBinary) {
            ObjectOutputStream ow = new ObjectOutputStream(os);
            ow.writeObject(obj); <== serialize all your objects here in
cycle
            ow.close();
        } else {
            String[] a_sParams = ...; <== array of encoded "param=value"
            OutputStreamWriter ow = new OutputStreamWriter(os);
            for (int i = 0;  i < a_sParams.length;  i++) {
                if (i > 0) {
                    ow.write("&");
                }
                ow.write(a_sParams[i]);
            }
            ow.close();
        }
    } // postParams()

-mg

-----Original Message-----
From: Lorin Davis [mailto:ldavis@pangaeainc.com] 
Sent: Thursday, April 10, 2003 6:10 PM
To: advanced-java@lists.xcf.berkeley.edu
Subject: [Advanced-java] Sending a Post from an applet to a servlet



Hi all 

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.

Here's how I do my current communications

myapplet.java 
  URL XMLURL = new
URL("Advanced-java@lists.xcf.berkeley.edu
http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java