Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use post-method with applet and servlet

 
Jorma Ikonen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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
 
juzi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
  • 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-
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic