//************************************************************************** // GetURLInfo.java: Applet // //************************************************************************** /* */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.*; //========================================================================== // Main Class for applet GetURLInfo // //========================================================================== public class GetURLInfo extends Applet implements Runnable, ActionListener { private Thread m_GetURLInfo = null; // PARAMETER SUPPORT: // Parameters allow an HTML author to pass information to the applet; // the HTML author specifies them using the tag within the // tag. The following variables are used to store the values of the // parameters. //---------------------------------------------------------------------- // Members for applet parameters // = //---------------------------------------------------------------------- private String m_Address = "http://www-ece.eng.uab.edu"; // Parameter names. To change a name of a parameter, you need only make // a single change. Simply modify the value of the parameter string below. //---------------------------------------------------------------------- private final String PARAM_Address = "Address"; // UI Controls private Button get_button; private TextField url_text; private TextArea url_info; private boolean threadflag = false; // GetURLInfo Class Constructor //---------------------------------------------------------------------- public GetURLInfo() { // TODO: Add constructor code here } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //---------------------------------------------------------------------- public String getAppletInfo() { return "Name: GetURLInfo\r\n" + "Author: David Green\r\n" + "Created with Microsoft Visual J++ Version 1.1"; } // PARAMETER SUPPORT // The getParameterInfo() method returns an array of strings describing // the parameters understood by this applet. // // GetURLInfo Parameter Information: // { "Name", "Type", "Description" }, //-------------------------------------------------------------------------- public String[][] getParameterInfo() { String[][] info = { { PARAM_Address, "String", "URL of where to get data" }, }; return info; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //---------------------------------------------------------------------- public void init() { // PARAMETER SUPPORT // The following code retrieves the value of each parameter // specified with the tag and stores it in a member // variable. //------------------------------------------------------------------ String param; // Address: URL of where to get data //------------------------------------------------------------------ param = getParameter(PARAM_Address); if (param != null) m_Address = param; resize(500, 300); // build UI this.add( new Label("URL to get:")); url_text = new TextField( m_Address, 40 ); url_text.setBackground(Color.white); url_text.setForeground(Color.black); this.add( url_text ); get_button = new Button("Get"); get_button.setForeground(Color.black); get_button.setBackground(Color.lightGray); get_button.addActionListener(this); this.add(get_button); url_info = new TextArea( "", 18,60, TextArea.SCROLLBARS_BOTH ); url_info.setBackground(Color.white); url_info.setForeground(Color.black); this.add( url_info); } // The start() method is called when the page containing the applet // first appears on the screen. The AppletWizard's initial implementation // of this method starts execution of the applet's thread. //---------------------------------------------------------------------- public void start() { if (m_GetURLInfo == null) { m_GetURLInfo = new Thread(this); threadflag = true; m_GetURLInfo.start(); } // TODO: Place additional applet start code here } // The stop() method is called when the page containing the applet is // no longer on the screen. The AppletWizard's initial implementation of // this method stops execution of the applet's thread. //---------------------------------------------------------------------- public void stop() { if (m_GetURLInfo != null) { threadflag = false; m_GetURLInfo = null; } // TODO: Place additional applet stop code here } // THREAD SUPPORT // The run() method is called when the applet's thread is // started. If your applet performs any ongoing activities without // waiting for user input, the code for implementing that behavior // typically goes here. For example, for an applet that performs // animation, the run() method controls the display of images. //---------------------------------------------------------------------- public void run() { while (threadflag) { try { repaint(); // TODO: Add additional thread-specific code here Thread.sleep(50); } catch (InterruptedException e) { // TODO: Place exception-handling code here in case an // InterruptedException is thrown by Thread.sleep(), // meaning that another thread has interrupted this one stop(); } } } // Event handler for button clicking public void actionPerformed(ActionEvent event) { // If the Get button was clicked, handle it if (event.getSource() == get_button) { url_info.setText(read_url_info()); } } private String read_url_info() { String result; try { URL url = new URL( (String) url_text.getText() ); URLConnection conn = url.openConnection(); result = conn.getURL().toExternalForm() + ":\n" ; result += " Content Type: " + conn.getContentType() + "\n"; result += " Content Length: " + conn.getContentLength() + "\n"; result += " Last Modified: " + new Date(conn.getLastModified()) + "\n"; result += " Expiration: " + conn.getExpiration() + "\n"; result += " Content Encoding: " + conn.getContentEncoding() +"\n"; // Read and print out the first five lines of the URL. result += "First five lines:\n"; InputStreamReader in = new InputStreamReader(conn.getInputStream()); BufferedReader br = new BufferedReader(in); for(int i = 0; i < 5; i++) { String line = br.readLine(); if (line == null) break; result += " " + line + "\n"; } } catch ( Exception e ) { result = e.toString() + "\n"; } return result; } }