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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

When dealing with servlets, you are no longer dealing with java, but HTML forms.

Therefore, your applet must do the fetch just like a form does,
this can be done using the GET method of HTTP.

In short, your information must be sent as name/value pairs
each pair should be separated by an ampersand (&)

Spaces are converted to plus signs (+) and all non alphanumerics (quotes, ampersands, etc) are converted into

thus custid, 10002
       name, Bob Wilkes


now postfix this information onto your url with a question mark

so url becomes

On the servlet, you can then retrieve the values normally during the "doGet()" method.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
My answer

Applet and Servlet Communication

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("");

Here we calling a script called (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 class. String theCGI = "";
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 = "";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI);

URLConnection c = theCGI.openConnection();
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.flush(); out.close();

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

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

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.

>> 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.