pass parameters from Applet to Servlet?

Posted on 1999-12-08
Medium Priority
Last Modified: 2013-11-24
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.
Question by:gnagabandi

Accepted Solution

wnross earned 100 total points
ID: 2266750
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.


Expert Comment

ID: 2266787
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("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();
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.

LVL 16

Expert Comment

ID: 2268077
>> 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.

Expert Comment

ID: 2269977
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.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question