Applet to Servlet Communication
Hello All,
I have written an applet which uses URLConnection object to invoke and post some data to the servlet. Basically a Post operation. I am using following piece of code:
URL url =new URL("http://localhost:8080/servlet/SimpleForm");
String qry = URLEncoder.encode("name") +"=" + URLEncoder.encode(qryString);// qryString is the variable containing the data
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
DataOutputStream dos =new DataOutputStream(uc.getOutputStream());
dos.writeBytes(qry);
dos.flush();
dos.close();
InputStreamReader in =new InputStreamReader(uc.getInputStream());
//rest of the code goes here
when i run this code using my applet, it gives me the following error:
java.io.FileNotFoundException: localhost:8080//servlet/SimpleForm
But when I invoke the servlet directly from the Browser's address bar by passing the data with the URL, the Servlet runs.
URL : http://localhost:8080/servlet/SimpleForm?name=xyz
Here the servlet itself is not invoked.
Can anyone help me on this, thanks in advance.
Ashok
[1706 byte] By [
ashok-hv] at [2007-9-26 1:15:16]

You're trying to do HTTP POST with java.net.URL and that's not straightforward. The browser URL you posted is HTTP GET and that's easy enough with java.net.URL.
GET has some limitations on the length of data passed after the ?, but your example seems not to be passing huge amounts of data so you'll probably be fine with GET.
Just build the URL in a String and pass it to the java.net.URL constructor!
try http://127.0.01:8080/servlet/SimpleFormregards
> try> > http://127.0.01:8080/servlet/SimpleForm> > regardsI think I missed on . , after correction this is the URL u have to try.. http://127.0.01:8080/servlet/SimpleFormregards
Thanks Jonas, atleast someone has responded to my question.
I think i have to rephrase my question. With the code below, i am trying post some data to the Servlet. When i run the applet and invoke the servlet, i get an error message saying:
java.io.FileNotFoundException: localhost:8080//servlet/SimpleForm
I am using Java Webserver and the log shows that the servlet is not invoked at all.
But if i do not post any data that is do not open any outputstream and just open an input stream and read the response of the servlet, then it works. I don't understand the logic here.
Can anybody throw some light on this.
URL url = new URL("http://localhost:8080/servlet/SimpleForm");
String qry = URLEncoder.encode("name") + "=" + URLEncoder.encode(qryString);// qryString is the variable containing the data
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
dos.writeBytes(qry);
dos.flush();
dos.close();
InputStreamReader in = new InputStreamReader(uc.getInputStream());
//rest of the code goes here
Add a main() method to your applet. Try to invoke the servlet from this main() method and run this little program from the command prompt.
If the Servlet invocation works, then we need to focus on what's wrong within the applet. If it doesn't work, we'll try to fix the servlet invocation.
hii think u have either used doPost or doGet Methodswhich will not work in case of app- servuse service method directly and i htink ur problem will be solvelet me know at a_ramesh77@rediffmail.com
hii think u have either used doPost or doGet Methodswhich will not work in case of app- servuse service method directly and i htink ur problem will be solvelet me know at a_ramesh77@rediffmail.com
Hi All,
First the good news, HTTP Post operation is working.
I like to thank Jonas, Kalipatnapu, steflead, and ramesh for your time and effort in solving my problem.
The way my applet is accessing the servlet is perfectly fine, i made the most silly mistake of writing doGet() and not doPost(). Normally i write my implementation in doGet() and call the doGet() from doPost() but this time I think i was overconfident and completely neglected this.
Once again Thanks to all.
Hi All,
First the good news, HTTP Post operation is working.
I like to thank Jonas, Kalipatnapu, steflead, and ramesh for your time and effort in solving my problem.
The way my applet is accessing the servlet is perfectly fine, i made the most silly mistake of writing doGet() and not doPost(). Normally i write my implementation in doGet() and call the doGet() from doPost() but this time I think i was overconfident and completely neglected this.
Once again Thanks to all.
Regards,
Ashok