Java Cookbook, Second Edition
Problem
You want an applet to run a CGI script. Solution
Just use showDocument( ) with the correct URL. Discussion
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. TryCGI.java
/** * 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="TryCGI.java">The Source</a>, Luke.</p> </body> </html> |