Lookup Applet

   This applet does searches of simple databases, like Phone Books. You can use this applet without any knowledge of Java by calling it from your HTML code. The database is a stored as a simple text file in your web directory. The applet down-loads this file into memory as it starts, so if you have a huge file this may take un-acceptably long. The number of fields in each record and the names to give them are defined in the APPLET tag in your html code.
Simple Phone Book
Complex Phone Book
Other Examples
How to use this Applet
Java Source Code, and complied Class files
Detailed explanation of Applet Parameters



Simple Phone Book

Heres a simple phone book example of a page that uses this applet.

This is the HTML code that calls the above example:

<HTML> <BODY> <BODY TEXT="#000000" BGCOLOR="#FFFFFF"> <CENTER><H2>Search Springfield Phone Book</H2></CENTER> <APPLET CODE="Lookup.class" WIDTH=580 HEIGHT=110> <param name="UrlOfDataFile" value="http://www.jdonohue.com/java/lookup/simple-phone.html"> <param name="fieldNames" value="Search for:,40"> <param name="fieldSeparatorCharacter" value="|"> <param name="foregroundColor" value="000000"> <param name="backgroundColor" value="FFFFFF"> </APPLET> <BR> <I>Click "Search" to see all the names, or enter a search term in one, or more, of the </I> <I>text boxes to constrain the results to only names that match it.</I> <!The Following is the Phone book data!> <B>This is the Springfield phone book data</B> <PRE> Homer Simpson 555-6528 Lisa Simpson 555-6528 ..... Stuff deleted ...... Dr. Nick Riviera 555-6022 Pr. John Frink 555-6024 </PRE> </BODY></HTML>

   This example defines only one field for each line. This is the simplist way to use the applet- because you don't need to put field separators in your data. The drawback is that you can only search the entire line of data for matches.   The "APPLET CODE" tag loads the "Lookup.class" Java applet and runs it. The "param name" tags supply information to the applet about where your file with the names and phone numbers is, and how to search and display the data. In the example above the line "Homer Simpson 555-6528" defines one record with that will be searched and displayed as a whole. The parameter "UrlOfDataFile" is set to the same html file that calls the applet. The applet will ignore any lines in the data file that are blank or that contain at least one HTML tags. You can therefore put HTML code above, and below your data. The parameter "fieldNames" specifies how the text box where search strings are entered is labeled. The "foregroundColor" and "backgroundColor" parameters define what color the applet should be. These should be the same as those defined in the HTML "BODY TEXT="#000000" BGCOLOR="#FFFFFF"" tag if you want the applet to blend into the page. The parameter "fieldSeparatorCharacter" is ignored in this case.




Complex Phone Book

Heres an complex phone book example where you can search individual fields, like first name or last name separately.

This is the HTML code that calls the above example:

<HTML> <BODY TEXT="#000000" BGCOLOR="#FFFFFF"> <CENTER><H2>Search Springfield Phone Book</H2></CENTER> <APPLET CODE="Lookup.class" WIDTH=620 HEIGHT=220> <param name="UrlOfDataFile" value="http://www.jdonohue.com/java/lookup/phone.html"> <param name="fieldNames" value="First Name,25,Last Name,30,Phone #,15"> <param name="fieldSeparatorCharacter" value=","> <param name="foregroundColor" value="000000"> <param name="backgroundColor" value="FFFFFF"> </APPLET> <PRE> Homer,Simpson,555-6528 Lisa,Simpson,555-6528 ..... Stuff deleted ...... Dr. Nick,Riviera,555-6022 Pr. John,Frink,555-6024 </PRE> </BODY></HTML>

   This example requires that the file has one record per line, and the fields in each line are delimited with a field separator character (like comma or "|"). The line "Homer,Simpson,555-6528" defines one record with three fields separated by a comma. The parameter "fieldNames" specifies that the first field is labeled "First Name", the second "Last Name", and the third "Phone #". The number following each name determines how many characters of that field will display on screen before truncating. Set this number to the maximum expected field size unless things are getting squeezed. The number of field names, and lengths specified in the "fieldNames" parameter has to match whats in your data or the Applet won't work. The parameter "fieldSeparatorCharacter" tells the applet that the fields are separated by commas.




Other Examples

A US Telephone Area Codes data base.
A Library data base.
A 2 letter ISO Country Codes data base.




How to use this Applet

  Copy the two required Java "*.class" files to the same directory on your web server as the HTML page that is going to call them. The class files are Lookup .class and Field.class

   Create the HTML page that will have the applet embedded in it. Create the file that will have your data. These two can be the same if you wish. Make sure the "UrlOfDataFile" parameter points to the full URL of your data file. Adjust the APPLET WIDTH, and HEIGHT tags so that all the GUI components fit, without too much empty space in the middle.




Java Source Code, and compilied Class files

Java Source Code:
Lookup.java
Field.java
Java Class files:
Lookup.class
Field.class

Note: Netscape does not download these files as binary. Use Internet Explore to download the class files.



Detailed explaination of Applet Parameters


UrlOfDataFile
Is the flat file with the records to search. This must be a URL of a file that the browser running the applet is allowed to retrieve (i.e. on the same web server as the applet). You can make this the same html file that calls the applet. This will give you a web page with a search applet at the top of the page, and a list of all the data below. When reading the flat file the applet only uses lines that do not contain the "<". You can therefore put html code in this file above and below the actual lines of data. This applet was meant to be used with a Web Server, and not run from your PC's hard-drive. Allthough it can sometimes work that way.

fieldNames
This is a list of the fields in each line of the data file. Each field is given a name to label it with. Following each field name is a number that specifies the number of characters to display in the results box for this field before truncation.

fieldSeparatorCharacter
This the character that delimites each field in the data file. Pick something that isn't likely to be in inside a field in your data file. If only one field name is given in the "fieldNames" parameter then this parameter is ignored. If only one fieldName parameter was given this parameter is ignored.

foregroundColor/backgroundColor
This is the colors to use for the foreground(text) and background in the applet The colors are specifed using the same notation as the HTML BODY tag.

Back to John Donohues's Java Examples Homepage
  Back to John Donohues's Homepage
Questions or Comments to:  
Last Updated 09/2/97