Assignment #9: Applets vs. Servlets
You will implement using both applets and servlets a program that finds
anagrams for words.
Two words are anagrams if they have the same letters in the same frequency.
For example, seven and evens are anagrams. In this program,
transfer everything to lower case. Your program will load words from a
dictionary (use the same dictionary as in the spell check assignment).
Then, for each word, you will compute its representative by sorting
the characters of the word to form a new string. seven and evens
have the same representative eensv. Obviously, to find all the
anagrams for a word, you compute its representative, and look for all other
words with the same representative. So as you load the dictionary, you
want to have a map in which the key is the representative, and the value
is a list of words with that representative. Once the map is computed,
answering anagram queries is trivial. You should probably get the logic
to work with a text-based application before you go to the applet and servlet
part.
For the applet, you will have a simple applet with a textfield for input,
a button to submit the request, and some place to put the output. Presumably,
after an answer is computed, you can specify another word. The applet will
have two significant limitations:
-
Because an applet runs in the browser, and you have no control over the
VM inside the browser, you should assume Java 1.1. Thus, do not use swing,
and do not use any Java 1.2 collections. You can use Hashtable
as your map and Vector as your list, and you must only use the
methods that were available as of Java 1.1 (the Javadocs will tell you
which ones were added in Java 1.2).
-
The dictionary will have to be stored on the server, probably at the same
codebase, and downloaded each time the applet is loaded. If you have a
real ISP that lets you do this, or an FIU homepage with normal modem, you
will see that the download time is signficant.
If the dictionary file is stored on the server
as dict.txt,
you will need a URL, which you can create using something
along the lines of
URL dict = new URL( getCodeBase( ), "dict.txt" );
after which you can get an InputStream to read.
Alternatively, an
applet parameter can specify the URL of the dictionary file.
For the servlet, you will have a simple HTML form, and the servlet will
handle the logic, and transmit back a new HTML page with the
results, and form space for another query. Since you control the servlet,
you could use the Java 1.2 collections, which might make things faster,
as there is no synchronization. Note also, that although several simultaneous
connections to the servlet would result in shared access of the collections
(the map is created only once, when the first connection to the servlet is made,
which is a significant performance win),
nobody is making any changes, so there is no thread safety problem. However,
you are not required to switch to Java 1.2 on the servlet side, and I mention
this merely to point out one advantage of servlets. Another advantage of
servlets is that the dictionary resides on the server and is never downloaded
to the client. To test your servlet, use servletrunner.
Submit your source code, floppy, and some screen snapshots.