CIT 597 Assignment 5: Calculator Servlet
Fall 2004, David Matuszek

Purposes of this assignment:

To introduce:

  • Servlets
  • Servlet sessions
  • Deployment descriptors
  • JSP

To reinforce:

  • HTML forms
  • JavaScript

General idea of the assignment:

Write a web application that functions as a simple hand calculator, but also keeps a "paper trail" of all your previous work.

Details:

Your program should consist of the following components:

An HTML page

This page is dynamically generated by JSP. It contains a trail of all the work you have done previously, for example something like this:

0.0
+ 2.5 = 2.5
+ 5.0 = 7.5
* 3.55 = 26.625
/ -3.0 = -8.875
   etc.

In addition, it should have the following HTML form elements: a text field in which you can enter numbers; add, subtract, multiply, and divide buttons; a clear button, to reset the current value to zero; and a clear all button, to reset the current value to zero and "erase" the paper trail (for when your page is getting too long and you want to start over).

Since the user should enter only numbers into the text field, use JavaScript to validate the numbers before you submit the form. (Hint: Google on "JavaScript validate input field".)

A servlet

The servlet should handle getting the request, calling some other class to do the work, and forwarding the result to JSP for formatting. Don't panic--it's just Java calling Java, you know how to do this.

Along the way, the servlet should do session tracking--it needs to keep track of the "paper trail" and the current value. I strongly recommend using the servlet tracking API, javax.servlet.http.HttpSession, as being the simplest and easiest way to do this.

A class to do the computations

Remember Model-View-Controller? The servlet itself is acting as the controller--it shouldn't have to do any computations (it delegates that work to some model class) or display any results (it delegates that work to the JSP).

A JSP page

The JSP page should create the HTML page from the information computed by the model class and forwarded to it by the servlet. Remember, Tomcat translates your JSP into ordinary Java.

A deployment descriptor

Required. Sorry.

Isn't this assignment kind of...er, stupid?

In "real life," your servlet would either be:

We aren't doing any of those things , and without a database (yet--we may do a tiny bit of database work later), there really just isn't much to do with a web application that we can't do much more easily with an ordinary application.

Due date

You should have your servlet up and running on our server, m174pc4.cis.upenn.edu, by Monday, November 15. It is possible that I will give another assignment before this one is due.