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

sborgianni@etnoteam.it sborgianni at etnoteam.it
Fri Apr 11 09:42:07 2003


You can also use a SOCKET connection like this:


Socket socket = new Socket(host, port);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

now you have to write your POST (request line is POST <uri> <HTTP Protocol> CRLF

bos.write(...);

then you have to write REQUESTED HEADERS like

content-lenght
content-type
host

finally you can send variables (use URLEncoder).

It seems to be more tedious but you can get more control over your code.

Stefano

>posted variables are just printed in the body of the request rather than in
>the URL of the request, so you'd do something like :
>
>
>import java.net.URLEncoder;
>
>
>URLConnection connection = XMLURL.openConnection();
>XMLURL.setDefaultUseCaches(false);
>XMLURL.setDoOutput(true);
>XMLURL.setDoInput(true);
>
>PrintWriter postWriter = new PrintWriter(XMLURL.getOutputStream());
>postWriter.print(variableName+"="+URLEncoder.encode(variableValue)+"&"+
>                 variableName2+"="+URLEncoder.encode(variableValue2)+"&"+
>                 ...
>                 );
>postWriter.print("\r\n");
>postWriter.flush();
>postWriter.close();
>
>InputStream is = connection.getInputStream();
> 
>InputStreamReader isr = new InputStreamReader( is );
>BufferedReader br = new BufferedReader( isr );
>
>
>that's sort of sketchy, because i can't find any exact code laying around
>here, only bits and pieces.  in the past when i tried to use posting from
>applets i found that netscape browsers (at least the older ones) cached the
>connection for the applet no matter the value of the useCaches.  so i've
>used querystring parameter passing exclusively for my applets, so that the
>browser treats each different connection to the webpage as a different item.
>
>-Jim P.
>
>
>
>> -----Original Message-----
>> From: Lorin Davis [mailto:ldavis@pangaeainc.com]
>> Sent: Thursday, April 10, 2003 5: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%20versio
>> n="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
>> 
>_______________________________________________
>Advanced-java mailing list
>Advanced-java@lists.xcf.berkeley.edu
>http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java