CMSC 10200 Lab — Spring 2006
Lab Assignment 5

PizzaXtreme--Revisited

Lab is due Sunday at 11:59pm. You must have an account on the CS tomcat server.

You are going to write an internet pizza server. A person selects the pizza size and chooses toppings, and your webpage returns a description of the pizza and the price. This is very much like lab2, except you will combine HTML (the lead webpage with the order form), JSP (provides the response to the order) and Java (the engine which does all the work.) You must check-out this hilarious internet pizza server.

I have written a little web application to create a spam-ilicious meal. I encourage you to snoop around my tomcat directory

	/stage/tomcat-cmsc/kaharris/webapps
so you can see what your site will look like at the end of the lab. You are also welcome to view the source code for the HTML and JSP pages and use what you would like. (Your pizza page better not look too much like my spam page though!)

Lab Overview

Writing the Java is the easy part of this lab, but you will also be writing an HTML pizza home page and JSP response page, which will call Pizza constructors and methods written in Java and stored elsewhere. In fact, you will be severely punished for any Java code (except very simple variable assignments) that makes its way in your .jsp!! Here are the steps you must follow

  1. Write and thoroughly test Java class code, which will be written in a package called pizza.
  2. Deploy your Java classes in your web application directory.
  3. Write a simple HTML page which passes a user's choice of pizza size and toppings to a JSP response page which merely echos the choices. A small set of choices among toppings (three or four) is sufficient.
  4. Prepare Tomcat for your pizza web server. (You must notify Tomcat you will be creating a new web application.)
  5. Re-write the JSP page to call the appropriate pizza constructor and methods to display a description of your pizza and its price. Test your HTML and JSP pages with the Java elements in your web application. At this point you have a very plain and simple pizza server--but it works!!
  6. Write a fancy HTML webpage to show off your Pizza Project. Now you have a great internet pizza server.

Evaluation

You will submit your Pizza project, which includes all your source code, and a README file with your name, and the http link to your project web application. You may also include any comments you would like me to read here.

Your lab will be evaluated on

  1. Thorough testing of all classes. This means each class you write must include a main routine. It is critical that you iron-out bugs before you try to deploy your code.
  2. Documentation of your code through public comments (javadoc).
  3. Code style. Your methods must be short, and must not refer to anything they ought not know about. This is made clearer in the discussion below on each class.
  4. Correct functioning of your web application. It should work without a fault. I will try to break your application!
  5. Code style in JSP. Your JSP must contain very minimal use of Java code.
  6. Aesthetic value of your application. Create a nice webpage!! You may use your lab2 page as a model. (You can use the name, logo and design pattern.)

1. Write the pizza package Java code

Create a directory lab5 for your work. You will add a new step to creating your project:

  1. Open Eclipse and set the compiler compliance to 5.0.
  2. Create a new project, Pizza. But in the Project Wizard, select the button
    	Create separate source and output folder.
       
  3. Eclipse will give you a source folder for your Java code, src.
  4. Create a package called pizza inside src.
  5. Import the following two enum classes into your pizza package
  6. Create javadoc documentation: Project -> generate javadoc..., for these two classes. You can view the generated documents with your browser, by going to the directory Pizza/doc and viewing the file index.html.
  7. You will be writing two Java classes in your pizza package, Pizza and PizzaJSP.
Eclipse will create a directory structure which will look as follows
		    Pizza
		/     |    \
	       bin   src   doc
	        |     |     |
	      pizza  pizza .html 
		|     |     
	     .class .java  
Eclipse creates the directory structure to mirror the package structure and separates out source code, compiled code and documentation.

Writing Pizza.java

All you need to know to write your Pizza.java about the enum classes Size and Topping are the found in the documentation I provided. These are smart enum classes that know their cost (price()) and description (toString()). You just need to know how to call these methods. If you ever find yourself needing to know the content of the enum class your code is bad and you will be severely punished.

methods and Exceptions:

Your Pizza class will have a contract with any user of it that the Size argument is non-null, and that if the Topping[] argument is null, then a Pizza object without toppings is created. Since this is part of the contract, it must be documented by you. The appropriate way in Java to handle a null value for the Size argument is to throw a NullPointerException, and you may do this--if you are able to test this using try ... catch.

Writing PizzaJSP.java

This is a wrapper class for your Pizza class. Your JSP page will call the constructor for this class, as well as its two methods: price() and toString(). The only thing this class needs to know about the enum classes Size and Topping is that there is a method fromString() which you can read about in the documentation.

Methods and Exceptions:

  1. private Pizza pizza;
  2. public PizzaJSP(String size, String[] toppings);
  3. public double price();
  4. public String toString();
Your JSP will recieve form information from your HTML page as a String (the size) and as an array String[] (the toppings). PizzaJSP simply translates these into appropriate arguments for Pizza and calls the Pizza constructor. How does PizzaJSP handle bizarre arguments? There are nasty issues about handling Exceptions. If PizzaJSP raises an Exception who would catch it? It is possible to write a JSP which looks for Exceptions and responds, but we will keep this application simple, since it is only used by your JSP:
  1. It is possible that String[] topping is null, but this must cause no Exception and no problems with your code.
  2. It is possible that some String in String[] toppings does not correspond to a Topping. This must cause no Exception and no problems with your code. Ultimately, it is the responsibility of the HTML page to ensure this does not happen.
  3. It is possible that String size is null or does not correspond to a Size. You must not pass a null for Size in the Pizza constructor. You may assume that the default Size is Size.SMALL. This should be the only reference to a Size or Topping object in your code. You must also document this decision about the default value. Ultimately it is the responsibility of the HTML page to ensure these problems do not arise.

2. Deploy your pizza package

Tomcat expects your web application directory has a very specific structure, so it can find the files it needs. Your directory hierarchy will look as follows under the Tomcat directory

	    /stage/tomcat-cmsc/
	    	   |
	        your-id
	  	   |
		webapps
        /          |           \
     WEB-INF    pizza.html     web
       |			|
    classes		     _pizza.jsp
       |
     pizza
       |
  files.class 
You may have other files and directories corresponding to other web applications (or class projects) as well Tomcat expects your web application to be organized this way

Do not continue unless your Java code is written and thoroughly tested.

Eclipse should have created a directory for your .class files

	bin/pizza/files.class
You want to create the above directory structure and make sure the permissions are set to allow everyone access to your directories, classes, and files. The steps below do the following: If you can do this, you can skip the instructions below. Otherwise, it is very important you follow each step very carefully.
  1. Move to /stage/tomcat-cmsc/your-id/webapps
  2. Create a directory WEB-INF. Set its permissions so everyone can enter the directory and see its contents:
      	chmod 755 WEB-INF
      
  3. Create a directory web and set its permissions so everyone can use this directory.
  4. Create your pizza.html file and set its permissions as follows
       	touch pizza.html
    	chmod 644 pizza.html
      
    The permission code is slightly different to allow anyone to read your code, but only you can write to it.
  5. Create _pizza.jsp inside the web directory, and set its permissions to allow everyone access to read it.
  6. Move inside WEB-INF and create a directory classes.
  7. Move inside classes. You are now ready to copy over your Java classes. (We will change the permissions on classes later.)
  8. You will need the path to the bin directory in your Pizza project. Inside classes copy the contents of the bin directory over:
     	cp -r path/bin .
      
    (There is a period at the end of the command.) This says to copy all files and directories below bin to the current directory (the period). You should now have your pizza package containing .class files inside your web application.
  9. Move back-up two directories, just outside WEB-INF
       	mv ../..
      
    This is shorthand for move-up two directories (the double-periods means the parent directory.)
  10. You should now be in the same directory as WEB-INF. You need to re-set the permissions on all files and directories below WEB-INF
      	chmod -R 755 WEB-INF
      
  11. DONE!!

3. Pass parameters from HTML page to JSP page

Write a simple HTML webpage in pizza.html which accepts a pizza size and several pizza toppings, and passes this information to a JSP webpage, _pizza.jsp under the web directory. The JSP page should just print the information it recieves.

The choice of pizza size should be one of three options (using HTML <select>): small, medium, and large. The choice of topping should be implemented using HTML <input type=checkbox ...>. Here is a nice link for creating HTML forms.

Warning: The values you set in your HTML webpage for each parameter must match the String descriptions in the enum classes. See the public documentation of these classes for what this value is. This documentation also displays the variety of toppings available.

You can retrieve parameters in your JSP webpage by calling one of two methods:

    <%
    String s = request.getParameter("parameter-name");
    String[] sa = request.getParameterValues("parameter-name"); 
    %>
The first method is used when the parameter has a single value (such as with pizza size), the second method is used when a parameter can have several values (such as with pizza toppings) and may be null, if no values were selected. The object request is created for your JSP and stores information your JSP may need, including parameter values. The opening and closing <% ... %> enclose a scriptlet, which is just lines of java code.

You print a value on the webpage your JSP builds by using the JSP expression type

   <%= value %>
What happens is that Tomcat converts this to a line of Java code
   out.print(value);
where out is an output stream to the browser (exactly like System.out is an ouput stream to the Console.) This means

4. Prepare Tomcat for Your Pizza Server

You need to let Tomcat know you have are deploying a new web application. Whenever you modify your class code for an application you should follow these steps. This ensures that Tomcat removes the old code from memory. (If you are only installing or changing an HTML or JSP page this is not necessary.)

  1. Open the Tomcat manager
    	https://tomcat-cmsc.cs.uchicago.edu/tomcat-manager/man/manager.cgi
      
    (you may be prompted for id and password.)
  2. If you are running Tomcat currently, Stop Tomcat and Remove your current application.
  3. Inform Tomcat of your new web application by entering the following data in the text fields on the manager page:
    	      Context name:  your-id
    	      Directory containing WEB-INF:  /your-id/webapps
       
    If all goes well, Tomcat should start running your application at /your-id/webapps.
  4. If you ever need to re-compile your .class code, you will need to Stop Tomcat, Remove your web application, then Reload after you have added the new files.

5. Re-Write JSP page to include Java pizza package

Your HTML and JSP pages are correctly passing parameters. You are ready to test the pizza package Java code. There are two main new parts you will need to add to your JSP page

  1. Inform Tomcat the location of the pizza package in your JSP
       	<%@ page import="pizza.*"%>
       
    This import directive (the <%@ %> is called a directive) can be placed anywhere in your JSP before you use any classes. It functions exactly like the Java import statement.
  2. Call appropriate constructors and methods to build a pizza object and calls its description and price.
Warning: If your JSP page includes any more Java code than calling constructors and methods, and assign statements for convenience, you will be heavily punished!! Your JSP should do no work, except create a webpage!!

Write your Pizza Webpage

Now, you can have some fun. There are three pizza sizes, and I have created thirty topping choices. Some of the toppings are premium toppings and this is reflected in the price. You should offer at least a dozen topping choices, with at least some premium toppings. You can create your own toppings and add them to the Topping class. If you really feel the need to offer kittens or goblins on your pizza, then by all means go ahead. It is your site.

All information you will need about the toppings I created you can find in the public documentation that you produced using javadoc from the two enum classes I provided for you.

Handin

Your lab is due Sunday at 11:59pm. You will submit your Pizza project folder. You must be inside this directory to submit. See handin.


Kenneth Harris