import java.io.* ; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Simple extends HttpServlet { static final boolean DEBUG = true ; static final String [] heads = {"name", "quest", "colour", "pet", "describe", "text"} ; static final int NAME = 0, QUEST = 1, COLOUR = 2, PET = 3, DESCRIBE = 4, TEXT = 5 ; public void init(ServletConfig config) throws ServletException { super.init(config); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter toClient = res.getWriter(); String [][] given = new String [heads.length][] ; Enumeration values = req.getParameterNames(); int i ; while(values.hasMoreElements()) { String name = (String)values.nextElement(); i = 0 ; while (!(name.equals(heads[i]))) i++ ; given[i] = req.getParameterValues(name); } toClient.println("
"); if (given[DESCRIBE] == null) { given[DESCRIBE] = new String [0] ; toClient.println("You don't describe yourself!") ; } else { toClient.print("You describe yourself as "); boolean wittyOrIntelligent = false, liar = false ; for (int j = 0 ; j < given[DESCRIBE].length ; j++) { String thisOne = given[DESCRIBE][j].trim() ; toClient.print(thisOne); if (thisOne.equals("witty") || thisOne.equals("intelligent")) wittyOrIntelligent = true ; else if (thisOne.equals("a liar")) liar = true ; if (j == given[DESCRIBE].length-2) toClient.println(" and "); else if (j < given[DESCRIBE].length-2) toClient.println(", ") ; } toClient.println("."); if (wittyOrIntelligent && !liar) toClient.println("Are you sure you aren't a liar?"); } toClient.println("
"); toClient.println("This is what you have to say for yourself:"); toClient.println("
"); toClient.println("" + given[TEXT][0].trim() + "
"); if (DEBUG) { toClient.println("