Date: Thu, 10 Apr 1997 18:20:37 -0700
Message-Id: <199704110120.SAA00801@puffin.eng.sun.com>
From: Marianne Mueller <mrm@Eng>
To: decinf@interbusiness.it
Subject: Re: Question about Applet Java interface with HTML page
> Date: Thu, 10 Apr 1997 11:26:45 +0200
> From: GIORGIO GIAMBIASI <decinf@interbusiness.it>
> Organization: D.E.C. INFORMATICA S.R.L.
> X-URL: http://java.sun.com/sfaq/index.html
>
> We are a italian SW House and we are interested in
> writing Java Applets for our HTML pages.
> We would like to know if it is possible to send
> information, fetched in an applet, into an HTML
> form, so we can submit information to another
> page.
> Thank you for attention,
> DEC Informatica Srl.
>
I'm not sure I understand the question exactly, but here's an example
of a tiny applet that posts data to a cgi program on a remote server.
To put data INTO the form elements on some web page, there would have
to be an API for accessing those form elements. I don't know of any
browsers that do that, although I bet you could do that with HotJava.
(See http://java.sun.com/products/HotJava/)
I think you're better off using Java to create the page and the GUI
elements on the page, since then you have access to all the GUI
elements on that page through the Java APIs, and you can then POST
that data to the server.
Marianne
--import java.applet.Applet; import java.awt.*; import java.net.*; import java.io.*; import java.util.*; public class PostExample extends Applet { private final String script = "/cgi-bin/nph-test-cgi"; private final String ctype = "application/octet-stream"; private String sdata = "Hello World"; private String rdata = ""; private String home; private int port;
public void init() { home = getDocumentBase().getHost(); port = getDocumentBase().getPort(); if (port == -1) port = 80; // System.out.println("home = " + home + ", port = " + port); }
public void start() { Socket sock; OutputStream outp; InputStream inp; DataOutputStream dataout; DataInputStream datain;
rdata = "";
//create a client socket try { sock = new Socket(home, port); } catch (Exception e) { rdata = e+" (socket: Host: "+home+"\tPort: "+port+")"; return; }
// Obtain output stream to communicate with the server try { outp = sock.getOutputStream(); inp = sock.getInputStream(); } catch (Exception e) { rdata = e+" (getstream)"; try { sock.close(); } catch (IOException ee) {}; return; }
try { dataout = new DataOutputStream(outp); datain = new DataInputStream(inp); } catch (Exception e) { rdata = e+" (Dstream)"; try { sock.close(); } catch (IOException ee) {}; return; }
// Send http request to server and get return data try { // HTTP header dataout.writeBytes("POST " + script + " HTTP/1.0\r\n"); dataout.writeBytes("Content-type: " + ctype + "\r\n"); dataout.writeBytes("Content-length: " + sdata.length() + "\r\n"); dataout.writeBytes("\r\n"); // end of header // POST data dataout.writeBytes(sdata);
boolean body = false; String line; while ((line = datain.readLine()) != null) { if (body) rdata += "\n" + line; else if (line.equals("")) // end of header body = true; } } catch (Exception e) { rdata = e+" (write)"; try { sock.close(); } catch (IOException ee) {}; return; }
// close up shop try { dataout.close(); datain.close(); } catch (IOException e) {}; try { sock.close(); } catch (IOException e) {}; } public void stop() {} public void paint(Graphics g) { StringTokenizer st = new StringTokenizer(rdata, "\n"); int line = 1, line_sp = getFontMetrics(getFont()).getHeight()+1; while (st.hasMoreTokens()) { g.drawString(st.nextToken(), 5, line*line_sp); line++; } } }
<title>Post Example</title> <hr> <applet code="PostExample.class" width=400 height=400> </applet> <hr> <a href="PostExample.java">The source, Luke.</a>