Recipe 18.6 Making an Applet Run a CGI Script


You want an applet to run a CGI script.


Just use showDocument( ) with the correct URL.


It doesn't matter what type of target your URL refers to. It can be an HTML page, a plain text file, a compressed tar file to be downloaded, a CGI script, servlet, or a JavaServer Page. In all cases, you simply provide the URL. The Java applet for this appears in Example 18-5.

Example 18-5.
/**   * Try running a CGI-BIN script from within Java.   */  public class TryCGI extends Applet implements ActionListener {      protected Button goButton;        public void init( ) {          add(goButton = new Button("Go for it!"));          goButton.addActionListener(this);      }        public void actionPerformed(ActionEvent evt) {          try {              URL myNewURL = new URL("http://server/cgi-bin/credit");                // debug...              System.out.println("URL = " + myNewURL);                // "And then a miracle occurs..."              getAppletContext( ).showDocument(myNewURL);            } catch (Exception err) {              System.err.println("Error! " + err);             showStatus("Error, look in Java Console for details!");          }      }  }

Since this is an applet, it requires an HTML page to invoke it. I used the HTML shown here:

<html> <head><title>Java Applets Can Run CGI's (on some browsers)</title></head> <body bgcolor="white"> <h1>Java Applets Can Run CGI's (on some browsers)</h1> <p>Click on the button on this little Applet for p(r)oof!</p> <applet code="TryCGI" width="100" height="30"> <p>If you can see this, you need to get a Java-powered(tm) Web Browser before you can watch for real.</p> </applet> <hr/> <p>Use <a href="">The Source</a>, Luke.</p> </body> </html>

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: