InfoSpace Example
Home ] Up ] [ InfoSpace Example ] Google Maps Example ] Get & Post ]

 

 

Here's an applet that can perform a CGI query by creating a URL with an appended query string, and asking the browser to show that URL in a new window.

It uses the InfoSpace web site to perform a lookup of a person or organization.

Did you forget to enable Java support?

Here's the code for this applet:

package networking;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.applet.AppletContext;
import java.io.UnsupportedEncodingException;

import java.net.URL;
import java.net.URLEncoder;
import java.net.MalformedURLException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *   Applet to look up a person via InfoSpace, using CGI GET.
 *
 *   @author Bryan J. Higgs, 10 October 1999
 */
public class PersonLookupApplet extends JApplet
{
  public void init()
  {
    // Lay out the components, once per applet activation.
    if (!m_laidOut)
    {
      JPanel contentPanel = new JPanel( new BorderLayout() );
      setContentPane(contentPanel);
      contentPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
      
/*
      // Title at the top
      JLabel announceLabel =
          new JLabel("Find a person using InfoSpace", JLabel.CENTER);
      announceLabel.setFont( new Font("SansSerif", Font.BOLD, 16) );
      announceLabel.setForeground( Color.magenta );
      contentPanel.add(announceLabel, BorderLayout.NORTH);
*/      
      // Labels, text fields and choices in the center
      JPanel centerPanel = createCenterPanel();
      contentPanel.add(centerPanel, BorderLayout.CENTER);
      centerPanel.setBorder(BorderFactory.createTitledBorder(
                                            "Find a person using InfoSpace") );
      
      // The find button at the bottom
      JPanel bottomPanel = new JPanel();
      contentPanel.add(bottomPanel, BorderLayout.SOUTH);
      bottomPanel.add(m_findButton);
      m_findButton.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event)
          {
            URL url;
            try
            {
              url = constructURL();
              // If it's valid, cause the browser to display the data
              if (url != null)
                getAppletContext().showDocument(url, "_blank");
            }
            catch (UnsupportedEncodingException ex)
            {
              ex.printStackTrace();
            }
          }
        }
      );
      
      m_laidOut = true;
    }
  }
  
  /**
   *   Creates the center panel, with all the labels
   *   and other components.
   */
  private JPanel createCenterPanel()
  {
    JPanel centerPanel = new JPanel( new GridLayout(4, 2) );
    
    JLabel firstNameLabel = new JLabel("First Name: ", JLabel.RIGHT);
    centerPanel.add(firstNameLabel);
    m_firstName.setBackground( Color.white );
    centerPanel.add(m_firstName);
    
    JLabel lastNameLabel = new JLabel("Last Name: ", JLabel.RIGHT);
    centerPanel.add(lastNameLabel);
    m_lastName.setBackground( Color.white );
    centerPanel.add(m_lastName);
    
    JLabel cityLabel = new JLabel("City: ", JLabel.RIGHT);
    centerPanel.add(cityLabel);
    m_city.setBackground( Color.white );
    centerPanel.add(m_city);
    
    JLabel stateLabel = new JLabel("State: ", JLabel.RIGHT);
    centerPanel.add(stateLabel);
    m_state.setBackground( Color.white );
    centerPanel.add(m_state);
    
    return centerPanel;
  }
  
  /**
   *   Constructs the CGI query URL from a base URL and
   *   a set of CGI parameters.
   */
  private URL constructURL() throws UnsupportedEncodingException
  {
    String urlString = "";
    URL url = null;
    try
    {
      // Construct the preset items for the CGI
      // script, whatever they mean
      urlString += constructItem("OTMPL", "/res/r1.html");
      urlString += constructItem("QFM", "N");
      urlString += constructItem("QK", "5");
      
      // Now, use the data the user specified in
      // the "form" part of the applet
      urlString += constructItem("QN", m_lastName.getText());
      urlString += constructItem("QF", m_firstName.getText());
      urlString += constructItem("QC", m_city.getText());
      // Get index of state name within Choice,
      // and get corresponding code
      String stateCode = m_states[m_state.getSelectedIndex()].code;
      urlString += constructItem("QS", stateCode);
      // Presumably, country?
      urlString += constructItem("KCFG", "US");
      
      // Convert the items string to
      // MIME "x-www-form-urlencoded" format.
      urlString = URLEncoder.encode(urlString, "UTF-8");
      
      // Construct the final absolute CGI Query URL
      url = new URL(INFOSPACE_URL + urlString);
    }
    catch (MalformedURLException e)
    {
      // Do nothing (the return URL will be null)
    }
    return url;
  }
  
  /**
   *   Constructs a CGI parameter string of the form "&code=value"
   */
  private String constructItem(String code, String value)
  {
    return "&" + code + "=" + value;
  }
  
  //// Private data ////
  private static final String INFOSPACE_URL =
      "http://kevdb.infospace.com/info/kevdb";
  
  // Data for all the 52 states
  private static final StateMapping[] m_states =
  {
    new StateMapping("al", "Alabama"),
    new StateMapping("ak", "Alaska"),
    new StateMapping("ar", "Arkansas"),
    new StateMapping("az", "Arizona"),
    new StateMapping("ca", "California"),
    new StateMapping("co", "Colorado"),
    new StateMapping("ct", "Connecticut"),
    new StateMapping("de", "Delaware"),
    new StateMapping("dc", "D.C."),
    new StateMapping("fl", "Florida"),
    new StateMapping("ga", "Georgia"),
    new StateMapping("hi", "Hawaii"),
    new StateMapping("id", "Idaho"),
    new StateMapping("il", "Illinois"),
    new StateMapping("in", "Indiana"),
    new StateMapping("ia", "Iowa"),
    new StateMapping("ks", "Kansas"),
    new StateMapping("ky", "Kentucky"),
    new StateMapping("la", "Louisiana"),
    new StateMapping("me", "Maine"),
    new StateMapping("md", "Maryland"),
    new StateMapping("ma", "Massachusetts"),
    new StateMapping("mi", "Michigan"),
    new StateMapping("mn", "Minnesota"),
    new StateMapping("ms", "Mississippi"),
    new StateMapping("mo", "Missouri"),
    new StateMapping("mt", "Montana"),
    new StateMapping("ne", "Nebraska"),
    new StateMapping("nv", "Nevada"),
    new StateMapping("nh", "New Hampshire"),
    new StateMapping("nj", "New Jersey"),
    new StateMapping("nm", "New Mexico"),
    new StateMapping("ny", "New York"),
    new StateMapping("nc", "North Carolina"),
    new StateMapping("nd", "North Dakota"),
    new StateMapping("oh", "Ohio"),
    new StateMapping("ok", "Oklahoma"),
    new StateMapping("or", "Oregon"),
    new StateMapping("pa", "Pennsylvania"),
    new StateMapping("ri", "Rhode Island"),
    new StateMapping("sc", "South Carolina"),
    new StateMapping("sd", "South Dakota"),
    new StateMapping("tn", "Tennessee"),
    new StateMapping("tx", "Texas"),
    new StateMapping("ut", "Utah"),
    new StateMapping("vt", "Vermont"),
    new StateMapping("va", "Virginia"),
    new StateMapping("wa", "Washington"),
    new StateMapping("wv", "West Virginia"),
    new StateMapping("wi", "Wisconsin"),
    new StateMapping("wy", "Wyoming")
  };
  
  // Whether we're already laid out
  private boolean   m_laidOut     = false;
  
  private JTextField m_firstName   = new JTextField(10);
  private JTextField m_lastName    = new JTextField(10);
  private JTextField m_city        = new JTextField(10);
  private Vector     m_stateVector = new Vector();
  {
    for (int i = 0; i < m_states.length; i++)
    {
      m_stateVector.add(m_states[i].name);
    }    
  }
  private JComboBox  m_state       = new JComboBox(m_stateVector);
  private JButton    m_findButton  = new JButton("Find");
  
  /**
   *   A simple nested static class to represent
   *   state codes and names, so we can construct
   *   the above array.
   */
  static class StateMapping
  {
    public StateMapping(String c, String n)
    {
      code = c;
      name = n;
    }
    
    final String code;  // State postal code
    final String name;  // State name
  }
}
 
The page was last updated February 19, 2008