CIT 597 Animal Game Suggestions
Fall 2005, David Matuszek

The Model

The model is the part that actually plays the Animals Game; it is not concerned in any way with communicating with the user. All communcation with the "outside world" is through method calls.This means that it can be used equally well in an applet, an application, or a web application; with a GUI, a text interface, or an HTML interface.

A model often consists of more than one class. In this case, you probably have some "master class" called something like AnimalsGame, that uses my BinaryTree class. You might have other classes as well.

When the servlet starts, it should create a new object of type AnimalsGame (or whatever you called it). This should only be done once. Remember that servlets have an init() method for one-time initializations.

To avoid dealing with special cases at startup, the Animals Game should be initialized with a nonempty binary tree. The one I usually use is:

Does it live in the water?
     Yes: frog
     No: horse

You can, of course, use any initial binary tree you like.

The binary tree grows only at the leaves. The animal name in the leaf node is replaced by a question; two children are added to the node, one containing the animal name that was in it, and the other containing the name of the new animal.

Does it live in the water?
     Yes: Does it eat people?
          Yes: shark
          No: frog
     No: horse

Notice that there is no need to replace a leaf node; you only need to replace its contents. In the above example, the value "frog" is replaced by the value "Does it eat people?"

Since (we will pretend) many thousands of people may be using our binary tree every second, and parts of it will be changing, we must synchronize access to the parts that can change. We can synchronize on individual nodes; we can synchronize on certain methods. Doing this correctly is a difficult problem; I don't expect anyone to really get it right, but I hope you will think about it.

The animals binary tree should persist (and grow) throughout the lifetime of the servlet. The most obvious (and probably the best) place to keep this binary tree is within the Animals Game class. It is worth noting, however, that the ServletContext object also has the desired lifetime, and can be used to store arbitrary objects..

The Controller

In this program you have an HttpServlet that acts as the controller. Its main job is to get a request from the user and give it to the Animals Game model; and to take the response from the Animals Game model and forward it to the view (JSP). It doesn't actually have to know anything about what kind of data is passing through it.

The controller also maintains session information. In this assignment, session information is simply knowledge about where this particular player is in the game. Many games can be played in a session, but only one at a time. Again, though, the controller can keep this information without actually knowing what it means.

Where do you keep session information? You have basically two choices:

The View

The JSP view takes a response and formats it into a Web page, which it then returns to the client (user). It does not return to the HttpServlet. The flow of control goes: User --> HttpServlet --> Model --> HttpServlet --> JSP --> User.

The user sees four or five different kinds of pages:

You could write a single JSP file that handles all these cases (it has to look at the information from the model to decide which kind of page to produce). The disadvantage is that the result can be quite lengthy.

You could have the controller look at the information from the model and decide to which JSP to forward the request. The disadvantage is that the controller has to know something about the program in order to make this decision.

You could write a JSP file that examines the information from the model, and chooses to which other JSP it will forward the request. The disadvantage is that you could end up with duplicated code in the various JSPs.

In any case, notice that when the controller forwards a request to a JSP, the JSP only gets two objects: the HttpRequest object and the HttpResponse object. Any information that the controller wants to send to the JSP must be put in the HttpRequest object. The JSP can, of course, import the Animals Game class, but this gives it access only to static information in that class.