[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("http://testserver/serlvets/myservlet?token=<?xml%20version="1.0"=
?><
XML>
<CMD>getXML</CMD></XML>");
URLConnection connection = XMLURL.openConnection();
InputStream is = connection.getInputStream() ;
InputStreamReader isr = new InputStreamReader( is );
BufferedReader br = new BufferedReader( isr );
while ((bufferIn = br.readLine()) != null) {
xml = xml + bufferIn;
}
then on
myservlet.java
there would be something like this
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String token = request.getParameter(xmlParam);
// now do something with the xml that was passed in
}
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.
In an HTLM you would use the <FORM> tags and <INPUT TYPE> tags to send
data.
Thanks.
_______________________________________________
Advanced-java mailing list
Advanced-java@lists.xcf.berkeley.edu
http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java