Use a search engine (this howto is deprecated)Tag(s): DEPRECATED


import java.applet.Applet;
import java.net.*;
import java.awt.*;

public class SearchApplet extends Applet {
 TextField searchParameter;
 Choice    searchEngine;
 Button    searchButton;

 public void init() {
   setBackground(Color.white);
   searchParameter = new TextField(20);
   add(searchParameter);
   searchEngine = new Choice();
   searchEngine.addItem("AltaVista");
   searchEngine.addItem("WebCrawler");
   searchEngine.addItem("Yahoo");
   searchEngine.select(0);
   add(searchEngine);
   searchButton = new Button("Search");
   add(searchButton);
   }

 // JDK1.02 style event handling
 public boolean action(Event e, Object o) {
   if (e.target.equals(searchButton)) {
     try {
       sendSearch();
     }
     catch (Exception e1) {
       showStatus("Exception caught:" + e1.toString());
     }
   }
   return true;
 }

public void sendSearch() throws Exception {
  String url;
  String searchString = searchParameter.getText();
  if (searchString.equals("")) {
    showStatus("Must enter a search string");
    return;
  }

  switch (searchEngine.getSelectedIndex()) {
    case 0: url =
    "http://www.altavista.com/web/results?q=";
     break;
    case 1: url =
     "http://msxml.webcrawler.com/info.wbcrwl/search/web/";
     break;
    case 2: url =
     "http://search.yahoo.com/bin/search?p=";
     break;
    default: showStatus("Invalid search engine selected.");
     return;
  }

  // encode the search data
  url += URLEncoder.encode(searchString);

  // launch the search engine
  showStatus("Connecting to search location " + url);
  getAppletContext().showDocument(new URL(url), "_top");
  }
}
Try it here.

It's not easy to call Google from an Applet but you can do it in application as seen in this HowTo.


blog comments powered by Disqus