// // UrlDemo class // part of the set of documents known as Java no sugar. // Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk // placed into the public domain by the author // import java.awt.*; import java.io.*; import java.net.*; import java.applet.*; public class UrlDemo extends Applet { //******************************************************************* // GLOBALS //******************************************************************* static final String USERDEF="USERDEF"; static final String HOSTPARAM="HOST"; //******************************************************************* // instance variables //******************************************************************* TextArea textarea; TextField textfield; Button btn_doit; boolean user_url = false; String url_string = null; //******************************************************************* // applet methods //******************************************************************* public void init() { GridBagConstraints c; GridBagLayout layout; String param; int gridy=0; //-----FIGURE OUT WHICH URL to use------------------------- param = getParameter(HOSTPARAM); if (param != null) { if (param.equals(USERDEF)) user_url = true; else url_string = param; } //--------BUILD THE USER INTERFACE------------------------- layout = new GridBagLayout(); this.setLayout( layout ); //------------------------add the text area---------------- if (user_url) { textfield = new TextField(30); c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.gridwidth=2; c.gridheight=1; c.fill = GridBagConstraints.BOTH; layout.setConstraints(textfield,c); this.add(textfield); gridy++; } //--------------------------------------------------------- textarea = new TextArea(); c = new GridBagConstraints(); c.gridx = 0; c.gridy = gridy; c.gridwidth=2; c.gridheight=2; c.fill = GridBagConstraints.BOTH; layout.setConstraints(textarea,c); this.add(textarea); //------------------------add button-------------------- btn_doit = new Button("query"); c = new GridBagConstraints(); c.gridx = 2; c.gridy = 0; c.gridwidth=1; c.gridheight=1; c.fill = GridBagConstraints.HORIZONTAL; layout.setConstraints(btn_doit,c); this.add(btn_doit); } public boolean action(Event event, Object arg) { if (event.target == btn_doit) { do_date_url(); return true; } return (false) ; } // // date is a safe choice since most httpd's implement this // private void do_date_url() { URL applet_url, cgi_url; String protocol, data_line; DataInputStream stream; //------------------------------------------------------------------ // clear out any text // textarea.setText(""); //------------------------------------------------------------------ // make sure applet is running from httpd // applet_url = this.getDocumentBase(); protocol = applet_url.getProtocol(); if ( ! protocol.equals("http") ) { textarea.appendText( "This applet must be run from a server\n" + "net.paranoid.geeks have crippled Java applets which\n" + "can only make network connections to the machine\n" + "from whence it came.\n" ); return; } textarea.appendText("Applet is on a server\n"); //------------------------------------------------------------------ // BUILD URL // try { if (user_url) { cgi_url = new URL( textfield.getText()); } else if ( url_string != null) { cgi_url = new URL( url_string); } else { cgi_url = new URL("http", applet_url.getHost(), applet_url.getPort(),"/cgi-bin/date"); } } catch (MalformedURLException e) { textarea.appendText("Error parsing " + e +"\n"); return; } catch (SecurityException e) { textarea.appendText("Security violation detected: " + e +"\n"); return; } textarea.appendText( "Attempting to use URL " + cgi_url.toString()+ "\n"); //------------------------------------------------------------------ // could use getContents, but dont understand how to use it. // textarea.appendText("\nthe output from the cgi-program is\n\n"); try { stream = new DataInputStream(cgi_url.openStream()); while ( (data_line = stream.readLine()) != null) { textarea.appendText(data_line +"\n"); } stream.close(); } catch(IOException e) { textarea.appendText("error reading data from url : "+ e); } } }
file: /Techref/language/JAVA/nosugar/comms/code/UrlDemo.java, 4KB, , updated: 1996/10/10 10:08, local time: 2024/11/16 08:52,
18.224.45.32:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.ecomorder.com/techref/language/JAVA/nosugar/comms/code/UrlDemo.java"> language JAVA nosugar comms code UrlDemo</A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Welcome to massmind.ecomorder.com! |
.