import java.applet.*; import java.io.*; import java.net.*; import java.awt.*; //Applet that connects to a file on its server to write three //lines of text into the file. //Note: Applets can only perform FileIO with files on the // server on which the Applet resides. //Note: To access a file on the server from an Applet you need // to do the following steps: // 1)Create an instance of URL pointing to the url of the file // 2)Create an instance of URLConnection class by calling the // openConnection() method of your URL instance from step 1 // 3)Create an instance of OutputStream class associated with your // URLConnection object of step 2 by calling its method getOutputStream() //NOTE: This Applet will invoke a Perl Server Application program and pass // to it the data it wants the Perl script to write to a file. public class myAppletOutputUsingServer extends java.applet.Applet { URL myURL; URLConnection myURLConnection; OutputStream myOS; PrintWriter myPW; String server = "newton.sci.csuhayward.edu"; int port = 200; //port number for ftp protocol ???80 PrintStream out; public void init() { String s; setBackground(Color.yellow); //Step 1&2: Create URL and URLConnection instances try{ //URL points to Perl Server Application s = "http://newton.sci.csuhayward.edu/~grewe/cgi-bin/WriteFile.cgi"; System.out.flush(); myURL = new URL(s); System.out.println("Created URL to " + myURL.toString()); }catch(MalformedURLException e) {System.out.println("Can't access URL"); System.out.println("String: " + e.toString()); System.out.println("Message: " + e.getMessage()); } try{ myURLConnection = myURL.openConnection(); //test to see if can do output System.out.println("ULRConnection able to do output? " + myURLConnection.getDoOutput()); System.out.flush(); //set so can do output myURLConnection.setDoOutput(true); //test to see if NOW can do output System.out.println("AFTER SET: ULRConnection able to do output? " + myURLConnection.getDoOutput()); System.out.flush(); //print out the URL string this connection attached to System.out.println("URL have URLConnection instance to: " + myURLConnection.getURL().toString()); System.out.flush(); }catch(IOException e) {System.out.println("Can't open connection to URL"); } //Step 3: Get OutputStream associated with URLConnection try { myOS = myURLConnection.getOutputStream(); } catch(IOException e) {System.out.println("Can't access URL outputstream"); System.out.println("String: " + e.toString()); System.out.println("Message: " + e.getMessage()); } //NOW YOU are going to compose a string containing 3 lines of // information you wish to store in the data file that the Perl // Server Application accesses. The format expected by the // Perl program is the same format as delivered by a CGI form. // Specifically, string1=value1&string2=value2&string3=value3 String time_string = java.util.Calendar.getInstance().getTime().toString(); s="string1=Lynne Grewe&string2=Your site is great&time=" + time_string; //Send the above string via a write operation to the output //stream associated with the Perl Application //I have decided to use the BufferedWriter class because //I like its write() and newLine() methods. Hence I need to convert //myOS (an Instance of OutputStream) to a BufferedWriter instance. //Step A: Convert myOS to an instance of OutputStreamWriter //Step B: Convert the instance of OutputStreamWriter to a // BufferedWriter instance. //STEP A OutputStreamWriter myOSW = new OutputStreamWriter(myOS); //STEP B BufferedWriter BW = new BufferedWriter(myOSW); //I have decided to use the PrintWriter class because //I like its println() methods. Hence I need to convert //myOS (an Instance of OutputStream) to a PrintWriter instance. //myPW = new PrintWriter(myOS); //Perform File Output with BufferedWriter instance //Write out 3 aribitrary lines to file /* DOESN'T DO ANYTHING myPW.println("hello Class"); System.out.println("hello Class"); myPW.println("2nd line"); myPW.println("3rd line"); myPW.flush(); System.out.flush(); */ try { BW.write(s, 0, s.length()); BW.flush(); System.out.println("Just wrote: " +s); System.out.flush(); }catch(IOException e) {System.out.println("can't write to file"); } //Close all streams // myPW.close(); try{ BW.close(); //myOSW.close(); //myOS.close(); } catch(IOException e) {System.out.println("can't close streams"); System.out.println("String: " + e.toString()); System.out.println("Message: " + e.getMessage()); } } }