0% found this document useful (0 votes)
46 views2 pages

Import Java

This Java applet code defines a form with a text area and button to allow the user to enter text. When the button is clicked, it retrieves the text from the form, sends it to a CGI script via HTTP POST, and displays any response. Specifically, it: 1. Sets up the GUI layout with a text area and "Write it!" button. 2. Handles the button click to get the text, send it to the CGI script, and read the response. 3. Sends the data using HTTP POST, setting the content type and length headers before writing the data to the connection's output stream.

Uploaded by

lichelles
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

Import Java

This Java applet code defines a form with a text area and button to allow the user to enter text. When the button is clicked, it retrieves the text from the form, sends it to a CGI script via HTTP POST, and displays any response. Specifically, it: 1. Sets up the GUI layout with a text area and "Write it!" button. 2. Handles the button click to get the text, send it to the CGI script, and read the response. 3. Sends the data using HTTP POST, setting the content type and length headers before writing the data to the connection's output stream.

Uploaded by

lichelles
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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