• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use post-method with applet and servlet

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm been trying to solve below problem around one week without success... Please, help me...
The problem is that I should transfer (chipered) data between applet and servlet by using post-method. The applet has code like below and it invokes the doPost-method on servlet, but I'm not able to get any data transferred. See codes below:
APPLET:
URL url = new URL(baseURL, servletName);
HttpURLConnection sc = (HttpURLConnection) url.openConnection();
sc.setRequestMethod("POST");
sc.setDoOutput(true);
sc.setDoInput(true);
sc.setUseCaches(false);
sc.setDefaultUseCaches(false);
sc.setAllowUserInteraction(false);
sc.setRequestProperty ("Content-Type", "application/octet-stream");
sc.setRequestProperty("Content-length", String.valueOf(10));
InputStream in = sc.getInputStream();
OutputStream out = sc.getOutputStream();
out.write("cipherdata".getBytes());
out.flush();
out.close();
...
in.close();

doPost-Method in SERVLET:
ServletInputStream in = request.getInputStream();
ServletOutputStream out = response.getOutputStream();
//next two line works ok...
System.err.println("Type: " + request.getContentType());
System.err.println("Content Length: " + request.getContentLength());
//but this do not return any bytes (jams) and also
//in.availabel()- function returns 0.
System.err.println("Read byte: " + in.read());
...

Everything should be ok, but just do not work. I'm quite beginner with Java and do not know is the problem my competence, web-server (Apache Tomcat/4.0-b1) or what. Please, help me.
Regards,
Jorma Ikonen
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the fact you use sc.doInput(true) after sc.doOutput(true) made the connection only reads from source.
Judy
 
Jorma Ikonen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Judy,
I really love you!
It seems that I have had very bad lucky. At the beginning I had the setDoInput first as described in http://www.keysolutions.com/ServletFAQ.nsf, but the example there didn't work, because they used URLConnection instead of HttpURLConnection (setRequestMethod is required, because URLConnection invokes only the doGet()-method and in.read() fails there). So, while I was tracking the first problem I caused accidentally the next one by slipping the setDoInput and setDoOutput upside down... life is life.
Many hugs, thanks, and greetings from the north pole.
-Jorma-
 
reply
    Bookmark Topic Watch Topic
  • New Topic