jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes How to use post-method with applet and servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to use post-method with applet and servlet" Watch "How to use post-method with applet and servlet" New topic

How to use post-method with applet and servlet

Jorma Ikonen
Ranch Hand

Joined: Sep 17, 2001
Posts: 49
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:
URL url = new URL(baseURL, servletName);
HttpURLConnection sc = (HttpURLConnection) url.openConnection();
sc.setRequestProperty ("Content-Type", "application/octet-stream");
sc.setRequestProperty("Content-length", String.valueOf(10));
InputStream in = sc.getInputStream();
OutputStream out = sc.getOutputStream();

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.
Jorma Ikonen

Joined: Sep 17, 2001
Posts: 2
I think the fact you use sc.doInput(true) after sc.doOutput(true) made the connection only reads from source.
Jorma Ikonen
Ranch Hand

Joined: Sep 17, 2001
Posts: 49
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.
Consider Paul's rocket mass heater.
subject: How to use post-method with applet and servlet
It's not a secret anymore!