
Server Side Java(tm) SDK
Pizza Sample Source Code
Robert B. Denny - Version 0.3 (experimental)
Back to overview
//tabs=4
//
// ==========
// PIZZA.JAVA
// ==========
//
// Sample server-side Java/WinCGI program for use with WebSite 1.1 and
// WebSite Professional. Taken after the "pizza" sample written in Visual
// Basic. This sample uses the WebSite Java CGI kit and the Java Developer's
// Kit version 2.0.
//
// Author: Robert B. Denny
//
// Edits:
// 12-Mar-96 rbd Initial edit, after PIZZA32.BAS
// 13-Mar-95 rbd Excise Java cup logo, cannot use!
// 18-Mar-96 rbd Use Java Powered logo, reformat a bit
// 18-Apr-96 rbd Reformat some more, smaller Java logo
// ==============================================================================
import WebSite.WinCGI.*;
import java.util.Date;
public class Pizza extends CGI {
private String name;
private String address;
private String city;
private String phone;
private String topping;
public void run() throws Throwable
{
//
// If we were called with the GET method, just send back
// the order form
//
if(Request.getMethod().equalsIgnoreCase("GET")) {
sendOrderForm();
return;
}
//
// Form POSTed. Get the form information into local
// vars for convenience.
//
name = Form.getField("name");
address = Form.getField("address");
phone = Form.getField("phone");
city = Form.getField("city");
//
// Get the topping checkboxes. Checkboxes are not sent if
// not checked, so the fields will be null, not empty!
//
topping = Form.getField("topping");
for(int i = 1; ; i++) {
String t = Form.getField("topping_" + i);
if(t == null) break;
topping += (", " + t);
}
if(topping == null)
topping = new String("None");
//
// Check the data, reject order if required fields are empty.
// NOTE: the fields will not be null, as they are in the form.
//
if(name.equals("") || address.equals("") || city.equals("") || phone.equals("")) {
rejectOrder("you didn't fill in one of the fields");
return;
}
//
// Looks good. Here is where we'd print the order in the kitchen.
// Since this isn't real, just send back a confirmation with the
// guaranteed delivery time.
//
sendHeader("Order Confirmation");
Response.println("<H1>");
Response.println("<img src=\"/wsdocs/images/jpower50-t.gif\" align=absmiddle>");
Response.println("Order Confirmation</H1>");
Response.println("Your order has been received, and appears valid.");
Response.println("<p>Here it is:");
Response.println("<PRE> Name: " + name);
Response.println("Street address: " + address);
Response.println(" Phone number: " + phone);
Response.println(" City: " + city);
Response.println(" Toppings: " + topping);
Response.println("</PRE>");
Response.println("If you have any corrections, please call 555-555-5555 immediately!");
sendFooter();
} // end run()
void rejectOrder(String reason) {
sendHeader("Order Rejected");
Response.println("<H1>Can't process your order</H1>");
Response.println("We can't process your pizza and Java order because ");
Response.println(reason + ".");
Response.println("<P>Please correct your order and re-send it.");
sendFooter();
}
void sendFooter() {
Response.println("<HR>");
Response.println("Click below to send mail to our order desk:<BR>");
Response.println("<A HREF=\"mailto:[email protected]\">");
Response.println("<ADDRESS><[email protected]></ADDRESS>");
Response.println("</BODY></HTML>");
}
void sendHeader(String title) {
Response.println("<HTML><HEAD><TITLE>" + title + "</TITLE></HEAD>");
Response.println("<BODY>");
}
void sendOrderForm() {
Date now = new Date(); // Current time
Date del = new Date(now.getTime() + 3600000); // 1 hour later
sendHeader("Pizza Order Form");
Response.println("<FORM METHOD=\"POST\" ACTION=\"/cgi-win/Pizza.clx\">");
Response.println("<IMG SRC=\"/wsdocs/images/jpower50-t.gif\" align=absmiddle>");
Response.println("<B>Godzilla's Pizza -- Internet Delivery Service</B><P>");
Response.println("At present, we accept internet orders only for medium (13\")");
Response.println("size pizzas. It is now " + now.getHours() + ":" + now.getMinutes());
Response.println(". You will have your pizza by ");
Response.println(del.getHours() + ":" + del.getMinutes() + ".");
Response.println("<PRE> Name: <INPUT SIZE=30 NAME=\"name\">");
Response.println("Street address: <INPUT SIZE=30 NAME=\"address\">");
Response.println(" Phone number: <INPUT SIZE=15 NAME=\"phone\">");
Response.println(" City: <SELECT NAME=\"city\">");
Response.println(" <OPTION SELECTED>Mesa (free)");
Response.println(" <OPTION>Chandler (free)");
Response.println(" <OPTION>Scottsdale ($15.00)");
Response.println(" <OPTION>Buckeye ($5.00)");
Response.println(" <OPTION>Tempe ($2.50)");
Response.println(" </SELECT> ");
Response.println("</PRE>");
Response.println("Which toppings would you like? <BR>");
Response.println("<OL>");
Response.println("<LI> <INPUT TYPE=\"checkbox\" NAME=\"topping\" VALUE=\"pepperoni\">");
Response.println(" Pepperoni.");
Response.println("<LI> <INPUT TYPE=\"checkbox\" NAME=\"topping\" VALUE=\"sausage\">");
Response.println(" Sausage.");
Response.println("<LI> <INPUT TYPE=\"checkbox\" NAME=\"topping\" VALUE=\"anchovies\">");
Response.println(" Anchovies.");
Response.println("</OL>");
Response.println("To order your pizza and free Java, press this button: <INPUT TYPE=\"submit\"");
Response.println("VALUE=\"Order Pizza\">");
Response.println("</FORM>");
sendFooter();
}
}
Back to overview
Java and other Java-based names are trademarks of Sun
Microsystems, Inc., and refer to Sun's family of Java-branded
technologies.