(1) For example,
//
// SameCharSets.java
//
// This servlet accept a set
of characters and search a Unix dictionary
// to find all words that can
be constructed by using exactly all
// characters in the set.
//
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SameCharSets extends HttpServlet
{
private boolean DEBUG = false;
String dictionaryFileName = "your_physical_directly\\words.txt";
Properties charSets = new Properties();
public void init(ServletConfig config)
throws ServletException {
super.init(config);
// Try to load the word
file to populate the Properties charSets,
// key is a set of character
in alphebetical order.
// value is the list of
all words constructed by using all char
// in the set.
try {
FileReader
fileReader = new FileReader(dictionaryFileName);
BufferedReader
bufferedReader = new BufferedReader(fileReader);
String inline
= null;
String key;
while ((inline
= bufferedReader.readLine()) != null) {
// process a line.
// Prepare key = inline in sorted alphabetical order. Use snail sort.
key = sortWord(inline);
if (charSets.getProperty(key) == null) {
charSets.put(key, inline);
}
else {
charSets.put(key, charSets.getProperty(key) + ", " + inline);
}
}
}
catch (FileNotFoundException
ignored) { }
catch (IOException ignored)
{ }
catch (NumberFormatException
ignored) { }
}
public void doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// get the
parameter value of word.
String word = request.getParameter("word");
String result;
if (word == null) {
// Print initial instruction and form.
out.println("<HTML>");
out.println("<HEAD><TITLE>Word
Constructor</TITLE></HEAD>");
out.println("<BODY
BGCOLOR=#CCCCFF>");
out.println("<H2
STYLE='COLOR:Blue'>Jumbo Word Constructor</H2>");
out.println("This
application allows you to enter characters and ");
out.println("output
words that are constructed of exactly these ");
out.println("characters.
For example, if you enter 'lpoo', it");
out.println("will
output 'loop', 'pool', 'polo', but not 'lop'.");
out.println("The
program uses a simple Unix dictionary.");
printForm(out);
out.println("</BODY></HTML>");
}
else if ((result
= charSets.getProperty(sortWord(word.toLowerCase()))) == null) {
// no word found.
out.println("<HTML>");
out.println("<HEAD><TITLE>Words not found.</TITLE></HEAD>");
out.println("<BODY BGCOLOR=#CCCCFF>");
out.println("<H2 STYLE='COLOR:Blue'>Jumbo Word Constructor: Not found</H2>");
out.println("Sorry, no word can be found for the characters: ");
out.println("<SPAN STYLE='COLOR:Blue'>" + word + "</SPAN><P>");
printForm(out);
if (DEBUG) {
for (Enumeration e= charSets.propertyNames(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
out.println(key + " ==> " + charSets.getProperty(key) + "<BR>");
}
}
out.println("</BODY></HTML>");
}
else { //
print result.
out.println("<HTML>");
out.println("<HEAD><TITLE>Words found.</TITLE></HEAD>");
out.println("<BODY BGCOLOR=#CCCCFF>");
out.println("<H2 STYLE='COLOR:Blue'>Jumbo Word Constructor: Result</H2>");
out.println("For the characters: ");
out.println("<SPAN STYLE='COLOR:Blue'>" + word + "</SPAN>, ");
out.println("the following words are found: ");
out.println("<SPAN STYLE='COLOR:Blue'>" + result + "</SPAN>.");
printForm(out);
out.println("</BODY></HTML>");
}
}
public void doPost(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
private static void printForm(PrintWriter
out) {
out.println("<FORM
METHOD='GET'>");
out.println("Please
enter your characters (no space in between):<P>");
out.println("<INPUT
TYPE=TEXT SIZE=10 NAME=word>");
out.println(" ");
out.println("<INPUT
TYPE=SUBMIT>");
out.println("</FORM>");
}
// sort the word inline in alphabetical
order using snail sort.
private String sortWord(String inline)
{
char[] temp;
char tempchar;
temp = inline.toLowerCase().toCharArray();
for (int i=0; i<temp.length-1;
i++) {
for (int j=i+1; j<temp.length; j++) {
if (temp[i] > temp[j]) {
tempchar = temp[i];
temp[i] = temp[j];
temp[j] = tempchar;
}
}
}
return String.valueOf(temp);
}
}