devxlogo

Getting Data Out of an Applet

Getting Data Out of an Applet

Question:
I have an applet with dialog boxes, text fields and buttons.What can I do with the user input? How do I get it out of the applet?What else can I do with this data?

Answer:
Perhaps the applet is a query for information. Then you need to findout what the user wanted and display it. But perhaps the user isentering information that you want, an order or a request for moreinformation by, perish the thought, mail or phone. Then you need toget that information out of the applet into your possession. That isthe focus of this question.

There is one thing an applet can’t do–save information onto theuser’s local disk. For example, you couldn’t write an applet thatlets the user do some calculation (say taxes, account records orretirement calculations) and then save the information on the userdisk, so that it can be reloaded next time they visit your site andrun the applet again. Some people view this as a limitation, butquite obviously it is a security measure. If an applet could saveinformation on the user’s disk, it could also overwrite existingfiles and create executable files with viruses in them. If you needto store user profiles, they must be stored on your server.

Any information that an applet saves must be saved on the sitefrom which the Java applet originates. How do you get it from theuser’s computer to your server? In a word, with sockets. A socket isa network connection between the applet and a server program runningon your server. Let’s suppose your applet takes an order from thecustomer. You naturally want to capture the information so you canfulfill the order.

The applet needs to open a socket to a server socket that isconstantly running on your computer. You may have a server socketprogram such as an SQL interface that is ready to accept queries andtransactions. If not, you can either write your own server program,or you can use the Common Gateway Interface (CGI) that is a standardpart of the Hypertext Transport Protocol (HTTP), the protocol of yourWeb server. It is actually quite easy to write a simple serverprogram using Java–much simpler than the same chore with C. But ittakes quite a bit of expertise to turn the simple prototype into areliable and secure production program, so it seems wiser to dealwith CGI, byzantine as it may be. Netscape and other vendors areworking on adding security (such as the secure socket layer) to theirbrowsers, and they will will focus their efforts on CGI.

To send your customer’s order information back to the server, your Javaapplet needs to contact a CGI script. That script must reside on the sameWeb server as the Java applet. A CGI script receives data that are sentback from the viewer of a Web page, and it can optionally send a responseback to the user. CGI scripts can be written in just about any programminglanguage. For historical reasons, Perl is commonly used, but you cancertainly write a Java application (not an applet) if there is a Javainterpreter running on the server.

Up to now, CGI scripts were used mainly to process HTML forms, not tointeract with Java applets, and they work well with forms. So whydon’t you just take your customer’s order with an HTML form? Thereare advantages to using a Java applet. The applet can compute totals,taxes and shipping charges. It can check if an item is in stock or ifa credit card is valid. All this is much more tedious withforms–you’d have go generate a maze of forms to cover eachsituation.

Unfortunately, right now CGI does not interact particularly well withapplets, and you have to program the interaction by hand. You have twomethods of getting information from the Java program to the CGI script. For a small amount of information, the command line argument is simpler.Suppose you want to find out whether an article is in stock. If thescript that can check stock is called /cgi-bin/order.pl, then youneed to execute the following Java code:

   Query q = "STOCK:" + item_name; // depends on your CGI script   URL u = new URL("www.where.com/cgi-bin/order.pl?" + q);   DataInputStream in = new DataInputStream(u.openStream());   String s = in.readLine(); // contains the first line of the answer

The CGI script then sends the answer back in some agreed format. Thiscan be text format and need not be HTML, so it can be designed to beeasy to parse. If you need to send more complex information, youneed a two-way socket connection, as follows:

     Socket s = new Socket("www.where.com/cgi-bin/order.pl", 80);          // 80 is the standard port number for http     DataOutputStream out = new DataOutputStream(s.getOutputStream());     DataInputStream in = new DataInputStream(s.getInputStream());     String outData = . . .;          // whatever you want to send to the script          // can be many lines     out.writeBytes("POST " + script + " HTTP/1.0
");     out.writeBytes("Content-type: " + ctype + "
");     out.writeBytes("Content-length: " + sdata.length() + "
");     out.writeBytes("
");     // end of header     out.writeBytes(outData);          // now listen to response     String inData = new String();     String line = null;     while ((line = datain.readLine()) != null)        inData += line + "
";

Now you know how to establish a two-way communication. You still needto design a protocol for the information to be sent back and forth,and, of course, you need to worry how to interface between the CGIscript and the database on the server side. Java, at least in itsinitial release, does not solve these problems.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist