You are on page 1of 2

import import import import

java.applet.Applet; java.net.*; java.io.*; java.awt.*;

public class Writeit extends Applet { Panel center; GridBagLayout gbl; GridBagConstraints gbs; TextArea info; Button send; public void init() { setLayout(new BorderLayout()); setBackground(Color.white); gbl = new GridBagLayout(); center = new Panel(); center.setLayout(gbl); gbs = new GridBagConstraints(); gbs.fill = GridBagConstraints.NONE; gbs.gridwidth = GridBagConstraints.REMAINDER; gbs.weightx = 1.0; showApp(); validate(); }

All we're doing in the code above is to set the form display. This could be done in a myriad of ways, and is merely a matter of preference. Now we'll handle the click event that will send the data to the URL:
public boolean action(Event e, Object o) { if (e.target.equals(send)) { try { writeMessage(); } catch (Exception e1) { } } return true; } public void writeMessage() throws Exception { String data; data = info.getText(); SendData(data); }

The data is retrieved and sent to the CGI file. On the client side, the data is written into the form element named "info". The getText function gets the data from the form element info. The data is then sent to the CGI via the SendData function.
public void showApp() { gbs.anchor = GridBagConstraints.WEST; center.add(info = addTextArea(3,50)); gbs.anchor = GridBagConstraints.CENTER;

center.add(send = new Button("Write it!")); add("Center", center); } public TextArea addTextArea(int rows, int cols) { TextArea ta = new TextArea(rows, cols); gbl.setConstraints(ta, gbs); ta.setBackground(Color.white); return ta; } public void SendData(String data) throws Exception { URL url = new URL("http","www.yourdomain.com", "/cgi-bin/wdwrite"); URLConnection con = url.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); con.setRequestProperty("Content-type", "text/plain"); con.setRequestProperty("Content-length", data.length()+"");

Once again, we have opened the URL using openConnection(). As this is using a CGI POST to send the data (as opposed to a GET in which the data would be appended to the end of the URL i.e. http://www.yourdomain.com/cgi-bin/writeit?myinfo), we are defining the Content-type to be text/plain.
PrintStream out = new PrintStream(con.getOutputStream()); out.print(data); out.flush(); out.close(); DataInputStream in = new DataInputStream(con.getInputStream()); String s; while ((s = in.readLine()) != null) { } in.close(); } }

You might also like