// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;

public class Remember extends Applet implements ActionListener {
    TextField a = new TextField();
    TextField b = new TextField();
    Button save = new Button();
    public void init() {
        load();
        save.setLabel("Save");
        save.addActionListener(this);
        add(a, null);
        add(b, null);
        add(save, null);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent ev) {
        save();
    }
    private String load() {
        StringBuffer res = new StringBuffer("");
        try {
            URL url = new URL("http://localhost/load.php");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.connect();
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String line = br.readLine();
                if(line != null) {
                    String[] vars = line.split(",");
                    a.setText(vars[0]);
                    b.setText(vars[1]);
                }
                con.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return res.toString();
    }
    private void save() {
        try {
            URL url = new URL("http://localhost/save.php");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            String info = "a=" + a.getText() + "&b=" + b.getText();
            con.setDoOutput(true);
            con.getOutputStream().write(info.getBytes());
            con.connect();
            if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.err.println("POST failed");
            }
            con.disconnect();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
