1 /* 2 * File: writefile.java 3 * 4 * This program demonstrates how to connect an applet to a CGI script 5 * in order to write a file on the web server. 6 * 7 */ 8 import java.awt.*; 9 import java.awt.event.*; 10 import java.net.*; 11 import java.io.*; 12 13 public class writefile extends java.applet.Applet 14 implements ActionListener 15 { 16 TextArea ta = new TextArea(); 17 TextField status = new TextField("Please type below", 40); 18 Button button = new Button("Write to Server"); 19 20 public void init() 21 { 22 setLayout(new BorderLayout(20,20)); 23 add("North",status); 24 add("Center",ta); 25 button.addActionListener(this); 26 add("South",button); 27 } 28 29 public void actionPerformed (ActionEvent evt) 30 { 31 String text_entered = ta.getText(); 32 33 String sdata; 34 sdata = "ta=" + text_entered; 35 36 status.setText("Processing Request . . ."); 37 String home = "osprey7.npac.syr.edu"; 38 String script = "/users-cgi/njm/writefile.pl"; 39 int port = 3768; 40 Socket s = null; 41 try 42 { s = new Socket(home,port); 43 DataOutputStream os = new DataOutputStream(s.getOutputStream()); 44 DataInputStream is = new DataInputStream(s.getInputStream()); 45 os.writeBytes("POST " + script 46 + " HTTP/1.0\r\n" 47 //+ "Content-type: application/x-www-form-urlencoded\r\n" 48 + "Content-type: text/plain\r\n" 49 + "Content-length: " + sdata.length() + "\r\n\r\n"); 50 status.setText("Text sent to host"); 51 os.writeBytes(sdata); 52 53 is.close(); 54 os.close(); 55 } 56 catch (Exception e) 57 { 58 showStatus("Error " + e); 59 status.setText("Errors!!!"); 60 if (s != null) 61 try 62 { s.close(); } 63 catch(IOException ex) {} 64 } 65 status.setText(sdata); 66 } 67 } 68 69 70 71 72