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/webappsso 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!)
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
- Write and thoroughly test Java class code, which will be written in a package called pizza.
- Deploy your Java classes in your web application directory.
- 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.
- Prepare Tomcat for your pizza web server. (You must notify Tomcat you will be creating a new web application.)
- 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!!
- Write a fancy HTML webpage to show off your Pizza Project. Now you have a great internet pizza server.
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
- Thorough testing of all classes. This means each class you
write must include a
mainroutine. It is critical that you iron-out bugs before you try to deploy your code.
- Documentation of your code through public comments (javadoc).
- 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.
- Correct functioning of your web application. It should work without a fault. I will try to break your application!
- Code style in JSP. Your JSP must contain very minimal use of Java code.
- 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:
- Open Eclipse and set the compiler compliance to 5.0.
- Create a new project, Pizza. But
in the Project Wizard, select the button
Create separate source and output folder.
- Eclipse will give you a source folder for your Java code, src.
- Create a package called pizza inside src.
- Import the following two
enumclasses into your pizza package
- 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.
- You will be writing two Java classes in your pizza package,
Pizza / | \ bin src doc | | | pizza pizza .html | | .class .javaEclipse creates the directory structure to mirror the package structure and separates out source code, compiled code and documentation.
All you need to know to write your
Pizza.java about the
the found in the documentation I provided. These are smart
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
class your code is bad and you will be severely punished.
methods and Exceptions:
public Pizza(Size s, Topping ts);
public int price();
public String toString();
Pizzaclass will have a contract with any user of it that the
Sizeargument is non-
null, and that if the
null, then a
Pizzaobject 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
nullvalue for the
Sizeargument is to throw a
NullPointerException, and you may do this--if you are able to test this using
try ... catch.
This is a wrapper class for your
Pizza class. Your
JSP page will call the constructor for this class, as well as its two
The only thing
this class needs to know about the
Topping is that there is a method
fromString() which you can read about in the documentation.
Methods and Exceptions:
private Pizza pizza;
public PizzaJSP(String size, String toppings);
public double price();
public String toString();
String(the size) and as an array
PizzaJSPsimply translates these into appropriate arguments for
Pizzaand calls the
Pizzaconstructor. How does
PizzaJSPhandle bizarre arguments? There are nasty issues about handling
catchit? 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:
- It is possible that
null, but this must cause no
Exceptionand no problems with your code.
- It is possible that some
String toppingsdoes not correspond to a
Topping. This must cause no
Exceptionand no problems with your code. Ultimately, it is the responsibility of the HTML page to ensure this does not happen.
- It is possible that
nullor does not correspond to a
Size. You must not pass a
Pizzaconstructor. You may assume that the default
Size.SMALL. This should be the only reference to a
Toppingobject 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.classYou 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
- All applications stored under /stage/tomcat-cmsc/your-id/webapps
- Underneath the application directory is the directory WEB-INF which contains a subdirectory classes which contains all packages needed by your web applications. Here you will place your pizza package.
- All home pages are placed directly underneath the application directory. For example, pizza.html is the home page of your Pizza project. You may also have index.html, which then acts as a home page for all your web applications.
- You may create subdirectories for other pages, such as your JSP pages. You see the web directory containing the JSP response page, _pizza.jsp. It is not necessary to have subdirectories like web, but if you don't use them, then your application directory is cluttered with HTML and JSP pages.
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.classYou 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:
- Create directories WEB-INF, classes, web and set all permissions to 755.
- Copy pizza package from your Project directory to the classes directory and set all permissions to 755.
- Create pizza.html and _pizza.jsp files and set their permissions to 644.
- Move to /stage/tomcat-cmsc/your-id/webapps
- Create a directory WEB-INF. Set its permissions so
everyone can enter the directory and see its contents:
chmod 755 WEB-INF
- Create a directory web and set its permissions so everyone can use this directory.
- Create your pizza.html file and set its permissions as follows
touch pizza.html chmod 644 pizza.htmlThe permission code is slightly different to allow anyone to read your code, but only you can write to it.
- Create _pizza.jsp inside the web directory, and set its permissions to allow everyone access to read it.
- Move inside WEB-INF and create a directory classes.
- Move inside classes. You are now ready to copy over your Java classes. (We will change the permissions on classes later.)
- You will need the path to the bin directory in your
Pizza project. Inside classes copy the contents of the bin
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.
- 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.)
- You should now be in the same directory as WEB-INF. You
need to re-set the permissions on all files and directories below
chmod -R 755 WEB-INF
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
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
requestis 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
outis an output stream to the browser (exactly like
System.outis an ouput stream to the Console.) This means
- value is converted to a
Stringby calling its
- Warning: Do not use a semicolon
in the JSP expression!! The following is BAD
<%= value; %>Why? What Java code will Tomcat produce? Is it correct Java syntax?
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.)
- Open the Tomcat manager
https://tomcat-cmsc.cs.uchicago.edu/tomcat-manager/man/manager.cgi(you may be prompted for id and password.)
- If you are running Tomcat currently, Stop Tomcat and Remove your current application.
- 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/webappsIf all goes well, Tomcat should start running your application at /your-id/webapps.
- 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
- 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.
- Call appropriate constructors and methods to build a pizza object and calls its description and price.
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
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
enum classes I provided for you.
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.