Link to home
Start Free TrialLog in
Avatar of gnagabandi
gnagabandi

asked on

pass parameters from Applet to Servlet?

I need my servlet to do specific processing depending on the user selection on an Applet. For this my applet has to pass parameters (like passing html form parameters to servlet) to servlet.
 
 Appreciate your swift responnce.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of wnross
wnross

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vladi21
vladi21

My answer

Applet and Servlet Communication
http://www.j-nine.com/pubs/applet2servlet/Applet2Servlet.html


----
http://tactika.com/realhome/javaht/java-n1.html#n4
Talk to a CGI/Servlet
From the client point of view, there is no difference talking to CGI or Servlet. There is two ways to send a request to a CGI. The GET method contains encoded parameters in the URL. A typical URL talking to CGI using the GET method would be: new URL("http://www.server.com/cgi-bin/aCGI.pl?name=Real&site=JAVA+HowTo");

Here we calling a script called aCGI.pl (a PERL script) passing the parameters name and site. Parameters are encoded, spaces are changed to "+" and special character to hexadecimal using a 3-letter escape sequence. Each parameter is delimited by the character "&". Habitually the encoding is done through the static method encode of the java.net.URLencoder class. String theCGI = "http://www.server.com/cgi-bin/aCGI.pl?";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI + encoded);

Once the URL is constructed, you call the CGI using the showDocument method. getAppletContext().showDocument(CGIurl);

The CGI will process the result and produce a page to be displayed.
The POST method allows the programmer to manipulate the data received from the CGI. First a connection is made to the CGI, an OutputStream is open to send the parameters (if any). Then InputStream is created to receive the result.String theCGI = "http://www.server.com/cgi-bin/aCGI.pl";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI);

URLConnection c = theCGI.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);
c.setRequestProperty("content-type","application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes(encoded);
out.flush(); out.close();

BufferedReader in =
   new BufferedReader(new InputStreamReader(c.getInputStream());

String aLine;
while ((aLine = in.readLine()) != null) {
   // data from the CGI
   System.out.println(aLine);
   }

You can't do some output then some input and do again some output. You must do all the output and then the input. There is no "dialog" between the client and the server. The client make a request and the server send back the result and close the connection.

--
POST:
http://www.javaworld.com/javatips/jw-javatip34.html 
http://www.javaworld.com/javatips/jw-javatip41.html 
>> When dealing with servlets, you are no longer dealing with java, but HTML forms.

When you are dealing with servlets, you ARE dealing with Java (for example you can pass Java objects to servlet).

only if you want to SIMULATE HTML forms you have to send real HTTP GET & POST requests.
In your applet, add the parameters with the url (encoded form) and the showdocument will pass the parameters to the servlets and in servlets, you can use the doget method to get the param values.