The following program shows you how to use Java to
search the Internet.
import java.awt.*;
import java.applet.Applet;
import java.applet.AppletContext;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
public class search extends Applet
{
Choice c;
Button b;
Frame f;
URL theURL;
Image img;
Image imgsec;
TextField tf;
String ex;
public void init()
{
img = getImage(getCodeBase(), "zeewolde.jpg");
tf = new TextField();
c.addItem("AskJeeves");
c.addItem("Dewa");
c.addItem("Dogpile");
c.addItem("Excite");
c.addItem("Filez");
c.addItem("Goto");
c.addItem("Highway61");
c.addItem("Hotbot");
c.addItem("Ilse");
c.addItem("Infoseek");
c.addItem("Identify");
c.addItem("MacUpdate");
c.addItem("Mamma");
c.addItem("McKinley");
c.addItem("Metacrawler");
c.addItem("Lycos");
c.addItem("Scrubtheweb");
c.addItem("Search");
c.addItem("Shareware");
c.addItem("Snap");
c.addItem("Tucows");
c.addItem("WebCrawler");
c.addItem("WebSitez");
c.addItem("Whatuseek");
c.addItem("Windowscentral");
c.addItem("Yahoo");
add(c);
add(tf);
add(b);
}
public void paint(Graphics g)
{
c.reshape(100, 20, 150, 20);
tf.reshape(100, 50, 150, 20);
b.reshape(255, 50, 35, 20);
g.setColor(Color.white);
g.draw3DRect(0, 0, size().width, size().height, true);
g.setColor(Color.black);
g.draw3DRect(3, 3, size().width - 7, size().height - 7, false);
g.fillRect(3, 3, size().width - 7, size().height - 7);
g.drawImage(img, 3, 3, this);
g.setColor(Color.white);
g.drawString("Search Engines", 10, 40);
g.drawString("Search for...", 10, 70);
}
public String getAppletInfo()
{
return "Java Search Engine 1.1 (jun 16 1998). Freeware made by Hans Wolters. For more info you can contact me by enail. h.wolters@gelrevision.nl";
}
public boolean action(Event event, Object object)
{
if (event.target instanceof Button)
{
object = c.getSelectedItem();
if (tf.getText().length() > 0)
gotoSearchUrl((String)object);
else if (tf.getText().length() < 1)
{
try
{
theURL = new URL("http://www." + object + ".com");
}
catch (MalformedURLException e)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Java Search Engine - by Hans Wolters (h.wolters@gelrevision.nl)");
}
}
return true;
}
void gotoSearchUrl(String string)
{
if (string == "Windowscentral")
{
try
{
theURL = new URL("http://www.windowscentral.com/cgi-bin/tips.cgi?OS=windows95&tip;=" + tf.getText());
}
catch (MalformedURLException e1)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Windowscentral");
}
if (string == "MacUpdate")
{
try
{
theURL = new URL("http://www.macupdate.com/subcategories.lasso?-db=software&-layout=search&-op=bw&keywords;=" + tf.getText() + "&-maxRecords=25&-sortfield=Title&-search=contains@-noResults=/pub/macupdate/error.lasso");
}
catch (MalformedURLException e2)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by MacUpdate");
}
if (string == "Shareware")
{
try
{
theURL = new URL("http://search.shareware.com/code/engine/Find?logop=and&cfrom;=quick&orfile;=True&hits;=25&search;=" + tf.getText() + "&category;=MS-Windows%28all%29");
}
catch (MalformedURLException e3)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Shareware");
}
if (string == "Tucows")
{
try
{
theURL = new URL("http://search.tucows.com/cgi-bin/webglimpse/webhome/server/htdocs/search?maxlines=20&maxfiles;=50&whole;=off&errors;=0&case;=off&query;=" + tf.getText());
}
catch (MalformedURLException e4)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Tucows");
}
if (string == "Filez")
{
try
{
theURL = new URL("http://search.filez.com/search.cgi?type=4&key;=" + tf.getText() + "&search;=1&rows;=20");
}
catch (MalformedURLException e5)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Filez");
}
if (string == "WebSitez")
{
try
{
theURL = new URL("http://search.websitez.com/search.cgi?key=" + tf.getText() + "&search;=1&type;=1&submit1;=Find");
}
catch (MalformedURLException e6)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by WebSitez");
}
if (string == "Dewa")
{
try
{
theURL = new URL("http://www.dewa.com/cgi-bin/search.cgi?k=" + tf.getText() + "&b;=o");
}
catch (MalformedURLException e7)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Dewa");
}
if (string == "Dogpile")
{
try
{
theURL = new URL("http://ms1.dogpile.com/search?q=" + tf.getText() + "&fs;=web&ss;=stop&to;=twenty");
}
catch (MalformedURLException e8)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by DogPile");
}
if (string == "AskJeeves")
{
try
{
theURL = new URL("http://www.askjeeves.com/AskJeeves.asp?ask=" + tf.getText() + "&qSource;=0&site;_name=Jeeves&metasearch;=yes");
}
catch (MalformedURLException e9)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by AskJeeves");
}
if (string == "Goto")
{
try
{
theURL = new URL("http://www.goto.com/d/search/;$sessionid$H4EWPLIAAAYTFQFIEENQPUQ?Keywords=" + tf.getText());
}
catch (MalformedURLException e10)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Goto");
}
if (string == "Scrubtheweb")
{
try
{
theURL = new URL("http://www.scrubtheweb.com/cgi-bin/search.cgi?action=Search&cat;=All&searchtype;=all&keyword;=" + tf.getText());
}
catch (MalformedURLException e11)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by ScrubTheWeb");
}
if (string == "Identify")
{
try
{
theURL = new URL("http://www.identify.com/identify.cgi?w=" + tf.getText() + "&st;=p");
}
catch (MalformedURLException e12)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Identify");
}
if (string == "Metacrawler")
{
try
{
theURL = new URL("http://www.metacrawler.com/crawler?general=" + tf.getText() + "&method;=0⌖=®ion;=0&rpp;=20&timeout;=5&hpe;=10");
}
catch (MalformedURLException e13)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Metacrawler");
}
if (string == "McKinley")
{
try
{
theURL = new URL("http://www.mckinley.com/search.gw?search=" + tf.getText() + "&c;=web&look;=magellan");
}
catch (MalformedURLException e14)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by McKinley");
}
if (string == "Whatuseek")
{
try
{
theURL = new URL("http://seek.whatuseek.com/cgi-bin/seek.alpha.go?db=db&defcmd;=find&disp;=all&grsz;=0&proximity;=rank&suffixproc;=off&thesaurus;=0&arg;=" + tf.getText());
}
catch (MalformedURLException e15)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by WhatUSeek");
}
if (string == "Highway61")
{
try
{
theURL = new URL("http://207.226.255.65/nph-seek.cgi?string=" + tf.getText() + "&bool;=and&new;_wins=on&speed;=reasonable&hits;=lots&yahoo;_cats=on&armadillo;=5&s;=wwwyx&dom;=2&c;=73701");
}
catch (MalformedURLException e16)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Highway61");
}
if (string == "Mamma")
{
try
{
theURL = new URL("http://www.mamma.com/cgi-bin/parsearch2?lang=1&timeout;=6&qtype;=0&query;=" + tf.getText() + "&summaries;=on");
}
catch (MalformedURLException e17)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Mama");
}
if (string == "Ilse")
{
try
{
theURL = new URL("http://www.ilse.com/?COMMAND=search_for&LANGUAGE;=NL&ANDOR;=OR&EXTRACT;=short&SEARCH;_FOR=" + tf.getText());
}
catch (MalformedURLException e18)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Ilse");
}
if (string == "Yahoo")
{
try
{
theURL = new URL("http://search.yahoo.com/search?p=" + tf.getText());
}
catch (MalformedURLException e19)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Yahoo");
}
if (string == "Infoseek")
{
try
{
theURL = new URL("http://www.infoseek.com/Titles?qt=" + tf.getText());
}
catch (MalformedURLException e20)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Infoseek");
}
if (string == "Hotbot")
{
try
{
theURL = new URL("http://www.hotbot.com/default.asp?MT=" + tf.getText());
}
catch (MalformedURLException e21)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Hotbot");
}
if (string == "Lycos")
{
try
{
theURL = new URL("http://www.nl.lycos.de/cgi-bin/pursuit?adv=0&cat;=lycos&npl;=matchmode%253Dand%2526adv%253D1&query;=" + tf.getText());
}
catch (MalformedURLException e22)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Lycos");
}
if (string == "WebCrawler")
{
try
{
theURL = new URL("http://webcrawler.com/cgi-bin/WebQuery?searchText=" + tf.getText());
}
catch (MalformedURLException e23)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by WebCrawler");
}
if (string == "Snap")
{
try
{
theURL = new URL("http://home.snap.com/search/directory/results/1,61,home-0,00.html?category=0-0-WW&keyword;=" + tf.getText());
}
catch (MalformedURLException e24)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Snap");
}
if (string == "Excite")
{
try
{
theURL = new URL("http://search.excite.com/search.gw?trace=1&look;=excite_netscape_us&sorig;=netscape&search;=" + tf.getText());
}
catch (MalformedURLException e25)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Excite");
}
if (string == "Search")
{
try
{
theURL = new URL("http://www.search.com/Infoseek/1,135,0,0200.html?QUERY=" + tf.getText());
}
catch (MalformedURLException e26)
{
System.out.println("Bad URL: " + theURL);
}
getAppletContext().showDocument(theURL, "Results by Search");
}
}
public search()
{
c = new Choice();
b = new Button("Go");
f = new Frame();
}
}
Kahindu